본문 바로가기

Learning/JAVA

조건문, 값 입력하기

  • 값을 입력받고 조건에 맞게 출력하기 (홀수 짝수, 나이별 입장판별) (Exam06)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package day01;
import java.util.Scanner//java안에 util안에 Scanner를 import한다.
public class Exam06 {
    public static void main(String[] args) {
        System.out.println("수를 입력하세요>>"); //scanner 이용해서 수를 입력받아서 출력하도록 명령.
        Scanner sc = new Scanner(System.in); //new라는 키워드를 이용해 스캐너에 값 입력. 키보드로 입력하도록.
        int su = sc.nextInt(); //a라는 "문자"를 넣게되면 오류 발생
        if(su%2==0) {
            System.out.println(su+ " 짝수");
        }else {
            System.out.println(su+ " 홀수"); 
        }
        
        System.out.println("나이를 입력하세요>>");
        int age = sc.nextInt(); //정수 입력 받음.
        if(age>=20) {
            System.out.println("입장가능");
        }else if(age>=15) { //&&를 쓸 필요 없음. 이미 20보다 작기때문에 여기로 내려온 거라서. else if 가 아니라 if라면 써야함.
            System.out.println("부모님과 동반입장");
        }else {
            System.out.println("입장 불가능");
        }
    }
cs

 

  • 점수와 커트라인을 입력받고 합격, 불합격 정하기 (Exam07)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package day01;
import java.util.Scanner;
public class Exam07 {
    public static void main(String[] args) {
        System.out.println("점수를 입력하세요>>"); //점수를 입력하라는 문장을 먼저 출력하도록
        Scanner i = new Scanner(System.in); //Scanner를 써서 키보드로 수를 입력받도록
        int point = i.nextInt(); //점수 값을 변수 point에 받도록. 스캐너 이름을 동일하게.
        
        System.out.println("커트라인을 입력하세요>>"); //커트라인을 입력하라는 문장을 먼저 출력하도록
        int cutline = i.nextInt(); //커트라인 값을 변수 cutline에 받도록. 스캐너 이름 동일하게.
        if(point>=cutline) {
            System.out.println("합격");
        }else {
            System.out.println("불합격");
        }
    }
}
cs

 

  • 국어, 영어, 수학 성적을 각각 입력하고 총점, 평균, 학점 구하기 (Exam08)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package day01;
import java.util.Scanner;
public class Exam08 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("이름 입력>>");
        String name = sc.next(); //nextInt 아님. 문자를 입력받는다.
        System.out.println("국어 점수를 입력하세요>>");
        int kor = sc.nextInt();
        System.out.println("영어 점수를 입력하세요>>");
        int eng = sc.nextInt();
        System.out.println("수학 점수를 입력하세요>>");
        int math = sc.nextInt();
        
        int total = kor+math+eng; //총점
        int avg = total/3//평균은 total 나누기 3
        
        System.out.println("이름:" +name);
        System.out.println("총점:" +total);
        System.out.println("평균:" +avg);
        String grade =""//문자열 변수 grade를 만들기
        
        if(avg>=90) {
            grade="A학점";  //system.out.println("A학점") 이런식으로 계속 쓰는 거 보단 grade 이용하고 맨 밑에 한번만 출력문 적어주면 됨.
        } else if(avg>=80) {
            grade="B학점";
        } else if(avg>=70) {
            grade="C학점";
        } else {
            grade="F학점";
        }
        System.out.println(grade);
    }
}
cs

 

  • tmp변수를 써서 큰 수, 작은 수, 양수값으로 나오는 두 수의 차 구하기 (Exam09)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package day01;
import java.util.Scanner;
public class Exam09 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        /*System.out.println("첫번째 수 입력>>");
        int num1 = sc.nextInt();
        System.out.println("두번째 수 입력>>");
        int num2 = sc.nextInt();
    
        if(num1>num2){
            System.out.println("큰 수: " +num1);
            System.out.println("작은 수: " +num2);
            System.out.println("두 수의 차: " +(num1-num2));
        }else if(num1<num2){
            System.out.println("큰 수: " +num2);
            System.out.println("작은 수: " +num1);
            System.out.println("두 수의 차: " +(num2-num1));
        }else {
            System.out.println("두 수는 같다");
        }
    }
    */
    System.out.println("첫번째 수 입력>>");
        int max = sc.nextInt();
        System.out.println("두번째 수 입력>>");
        int min = sc.nextInt();
        if(min>max) {
            int tmp = max; //임시로 max의 값을 저장할 정수형 변수 tmp 선언.
            max = min; //min의 값이 max에 들어감.
            min = tmp; //아까 max의 값을 임시로 저장했던 tmp의 값이 min에 들어감.
        }
        System.out.println("큰 수: " +max);
        System.out.println("작은 수: " +min);
        System.out.println("두 수 차: " +(max-min));
    }
}
cs

 

  • 두 자리 수를 10의 자리, 1의 자리 떼어서 3,6,9 박수치기 (Exam10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package day01;
import java.util.Scanner;
public class Exam10 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("1~99사이 정수를 입력하세요");
        int num = sc.nextInt();
        
        int a = num/10;
        int b = num%10;
        
        /*
        int c = a%3;
        int d = b%3;
                
        if((c==0)&&(d==0)) {
            System.out.println("박수짝짝");
        }else if((c==0)&&(d!=0)) {
            System.out.println("박수짝");
        }else if((c!=0)&&(d==0)) {
            System.out.println("박수짝");
        }else {
            System.out.println("박수없음");
        }
        */
        
        int cnt=0;
 
        if(a!=0&&a%3==0) {
            cnt = cnt+1;
        }
        if(b!=0&&b%3==0) {
            cnt = cnt+1;
        }
        if(cnt==2) {
            System.out.println("박수짝짝");
        }else if(cnt==1) {
            System.out.println("박수짝");
        }else {
            System.out.println("박수없음");
        }
    }
}

 

  • 전위연산, 후위연산 (Exam11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package day01;
public class Exam11 {
    public static void main(String[] args) {
        int n1 = 5;
        int n2 = ++n1; //n1에서 1 증가한 값을 n2에 넣기.
        System.out.println("n1: "+n1); //6
        System.out.println("n2: "+n2); //6
        System.out.println("======");
        
        int n3 = n1++//6을 n3에 넣고, 빠져나오면서 n1이 7이 됨.
        System.out.println("n1: "+n1); //7
        System.out.println("n3: "+n3); //6
        System.out.println("======");
        
        int num1=7;
        int num2=--num1; //num1=6, num2=6
        int num3=num1--//num1=5, num3=6
        System.out.println("num1: "+num1); //5
        System.out.println("num2: "+num2); //6
        System.out.println("num3: "+num3); //6
        
        int a=50, b=3;
        int c;
        a++//빠져 나오면서 a=51
        ++b; //b=4
        c=a++ + ++b; //c=51+5=56 값을 계산하고 난 뒤에 빠져나오면서 a=52
        System.out.println("a: "+a); //52
        System.out.println("b: "+b); //5
        System.out.println("c: "+c); //56
        System.out.println("======");
 
        int aa=10, bb=3;
        int cc;
        --aa; //a=9
        bb++//빠져나오면서 bb=4
        cc=aa-- + ++bb; //cc=9+5=14, bb=5, aa=8
        System.out.println("aa: "+aa); //8
        System.out.println("bb: "+bb); //5
        System.out.println("cc: "+cc); //14
        
    }
}

 

  • switch문 (Exam12)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package day01;
import java.util.Scanner;
public class Exam12 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("1.자바 2.데이터베이스 3.안드로이드 4.기타");
        System.out.println("과목번호 선택>>");
        int num = sc.nextInt();
        /*
        if(num==1) {
            System.out.println("자바공부");
        }else if(num==2) {
            System.out.println("데이터베이스공부");
        }else if(num==3) {
            System.out.println("안드로이드공부");
        }else if(num==4) {
            System.out.println("기타 공부");
        }else {
            System.out.println("공부합시다.");
        }
        */
        switch(num) {
            case 1 :
                System.out.println("자바공부");break//break반드시 붙이기. 있는지 꼭 확인하기.
            case 2 :
                System.out.println("데이터베이스공부");break;
            case 3 :
                System.out.println("안드로이드공부");break;
            case 4 :
                System.out.println("기타공부");break;
            default :
                System.out.println("공부합시다.");
        }
    }
}
 
cs

 

  • 두 수를 입력하고 연산자에 따라 다른 결과값 출력하기 (Exam13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package day01;
import java.util.Scanner;
public class Exam13 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("첫번째 수 입력>>");
        int a=sc.nextInt();
        System.out.println("두번째 수 입력>>");
        int b=sc.nextInt();
        System.out.println("연산자입력");
        String c=sc.next();
        
        switch(c) { //여기에는 쌍따옴표 안함. 변수 명만 적으면 됨.
            case "+" :
                System.out.println("덧셈결과: " +(a+b));break;
            case "-" :
                System.out.println("뺄셈결과: " +(a-b));break;
            case "*" :
                System.out.println("곱셈결과: " +(a*b));break;
            case "/" :
                System.out.println("나눗셈결과: " +(a/b));break;
            case "%" :
                System.out.println("나머지결과: " +(a%b));break;
            default :
                System.out.println("연산자가 틀렸습니다.");
        }
          if (c.equals("+")) { //문자열 값을 비교할때는 equals() 사용. 숫자 int는 ==임.
            System.out.println("덧셈결과: " +(a+b));
        }else if (c.equals("-")) {
            System.out.println("뺄셈결과: " +(a-b));
        }else if (c.equals("*")) {
            System.out.println("곱셈결과: " +(a*b));
        }else if (c.equals("/")) {
            System.out.println("나눗셈결과: " +(a/b));
        }else if (c.equals("%")) {
            System.out.println("나머지결과: " +(a%b));
        }else {
            System.out.println("연산자가 틀렸습니다.");
        }
        
        
    }
}
cs