Binary World

JAVA CLASS : 조건문(if) 본문

개발자의 길/JAVA

JAVA CLASS : 조건문(if)

모쿠 2016. 12. 29. 11:30

<JAVA PRACTICE>


*IfMain01


public class IfMain01 {

public static void main(String[] args){

System.out.println("if-else 문");

int x = 100;

if (x > 0){

System.out.println("양수");

} else {

System.out.println("0보다 크지 않음");

}

System.out.println("첫번째 if-else 끝");

System.out.println("==================");

int y = 100;

if(y > 0){

System.out.println("양수입니다!");

} else {

System.out.println("0보다 크지 않습니다.");

}

} // end main()

} // end class IfMain01



*IfMain02


import java.util.Scanner;


public class IfMain02 {


public static void main(String[] args) {

System.out.println("if-else 문과 삼항연산자");

Scanner sc = new Scanner(System.in);

System.out.println("첫번째 수를 입력:");

int x = sc.nextInt();

System.out.println("두번째 수를 입력:");

int y= sc.nextInt();

System.out.println("입력한 수 : " + x + ", " + y);

int big;

if(x > y){

// 조건이 참일 때 실행되는 부분

big = x;

} else {

// 조건이 거짓일 때 실행되는 부분

big = y;

}

System.out.println("큰 수 :" + big);

System.out.println("=================");

// 삼항연산자: (조건식) ? (값1) : (값2)

// (조건식)이 참이면 (값1)을 선택

// (조건식)이 거짓이면 (값2)를 선택

int big2 = (x > y) ? x : y;

System.out.println("big2 = " + big2);

} // end main()

} // end class Ifmain02



*IfMain03


public class IfMain03 {


public static void main(String[] args) {

System.out.println("if - else if - else문");

int x = 100;

if (x > 0) { // 조건1

// 조건1이 참일 때 실행되는 부분

System.out.println("양수");

} else if (x == 0) { // 조건 2

// 조건2가 참일 때 실행되는 부분

System.out.println("0");

} else {

// 조건1, 2 모두 거짓일 때 실행되는 부분

System.out.println("음수");

}

System.out.println("if - else if - else 끝");

} // end main()


} // end class IfMain03


*IfMain04


import java.util.Scanner;


public class IfMain04 {


public static void main(String[] args) {

// if - else if - else 연습

Scanner sc = new Scanner(System.in);

System.out.println("점수를 입력:");

int average = sc.nextInt();

// average가 90 점 이상이면 "A"를 출력

// average가 80 점 이상이면 "B"를 출력

// average가 70 점 이상이면 "C"를 출력

// average가 70 점 미만이면 "F"를 출력

System.out.println("평균 점수는 :");

if(average >= 90){

System.out.println("A");

} else if(average >= 80 ){

System.out.println("B");

} else if(average >= 70 ){

System.out.println("C");

} else {

System.out.println("F");

}

}




*IfMain05


import java.util.Scanner;


public class IfMain05 {

public static void main(String[] args){

// 문자 하나를 입력받아서 그 글자가

// 1) A ~ Z이면, "영대문자"라고 출력

// 2) a ~ z 이면, "영소문자"라고 출력

// 3) 0 ~ 9 이면, "숫자"라고 출력

// 4) 그 외 경우는, "몰라요~"라고 출력

Scanner sc = new Scanner(System.in);

System.out.println("문자를 입력하세요 :");

char ch = sc.nextLine().charAt(0);

if ( ch >= 'A' && ch <= 'Z')

{

System.out.println("영대문자");

else if ( ch >= 'a' && ch <= 'z')

{

System.out.println("영소문자");

}

else if ( ch >= '0' && ch <= '9')

{

System.out.println("숫자");

}

else

{

System.out.println("몰라요~");

}

} // end main()


} // end class IfMain05



* IfMain06


import java.util.Scanner;


public class IfMain06 {


public static void main(String[] args) {

// 정수 하나를 입력받아서,

// 입력받은 정수가 짝수이면, "짝수" 출력

// 입력받은 정수가 홀수이면, "홀수" 출력

Scanner sc = new Scanner(System.in);

System.out.println("숫자를 입력하세요:");

int num = sc.nextInt();

sc.close();

if ((num % 2) == 0 ){ //나머지가 0이면 짝수

System.out.println("짝수");

}

else

{ // 1이면 홀수

System.out.println("홀수");

}

System.out.println("================");

// 삼항연산자 사용

String result = (num % 2 == 0) ? "짝수" : "홀수";

// 위의 if문과 똑같은 내용


} // end main()


} // end class IfMain06


 

* IfMain07


import java.util.Scanner;


public class IfMain07 {


public static void main(String[] args) {

// 정수 두개를 입력 받아서,

// 두 수의 차를 출력

// x > y -> x - y

// x < y -> y - x

Scanner sc = new Scanner(System.in);

System.out.println("첫번째 수를 입력 :");

int a = sc.nextInt();

System.out.println("두번째 수를 입력 :");

int b = sc.nextInt();

int result1;

sc.close();

if( a > b ){

result1 = a - b;

}

else

{

result1 = b - a;

}

System.out.println("두 수의 차는 :" + result1);

System.out.println("====================");

int result2 = ( a > b) ? (a - b) : (b - a);

System.out.println("두 수의 차는 :" + result2);


}




* IfMain08


public class IfMain08 {


public static void main(String[] args) {

// 중첩 if문

int num = 100;

if (num % 2 == 0){

System.out.println("짝수");

if ( num % 4 == 0){

System.out.println("4의 배수");

} else {

System.out.println("4의 배수가 아닌 짝수");

}

} else {

System.out.println("홀수");

}


} // end main()


} // end class IfMain08 


* IfMain09


public class IfMain09 {


public static void main(String[] args) {

// SCE(Short-circuit evaluation: 짧은 계산)

// Lazy Evaluation(게으른 계산)

// A && B를 계산할 때 A가 false이면, B를 계산하지 않음

// A || B를 계산할 때 A가 true이면, B를 계산하지 않음

int x = 0;

int y = 0;

if((x += 10) < 0 && (y +=10) > 0){

System.out.println("1");

} else {

System.out.println("2");

}

System.out.println("x = " + x);

System.out.println("y = " + y);

// 이미 x에서 false가 나와서 뒤에 y값에 대한 계산을 하지 않음

// 2 

        // x = 10

// y = 0

System.out.println("===============");

x = 0;

y = 0;

if((x += 10) > 0 || (y +=10) > 0){

System.out.println("1");

} else {

System.out.println("2");

}

System.out.println("x = " + x);

System.out.println("y = " + y);

// x 가 true이기 때문에 y값을 계산하지 않음

// 1

// x = 10

// y = 0

} // end main()



* GameMain02(가위 바위 보 게임)


import java.util.Scanner;


public class GameMain02 {


public static void main(String[] args) {

double r = Math.random(); // 0과 1사이의 난수를 발생

System.out.println(r); // 0 <= r <1

r = (3 * r + 1); //1 <= r < 4

System.out.println(r);

int com = (int) r; 

System.out.println(com);

System.out.println("가위바위보 게임!!");

System.out.println("가위(1), 바위(2), 보(3)중 하나를 입력하세요 :");

Scanner sc = new Scanner(System.in);

int user = sc.nextInt();

if (user == com){

System.out.println("비겼습니다.");

} else if ((user == 1) && (com == 2)){

System.out.println("컴퓨터는 바위를 냈습니다.");

System.out.println("당신이 졌습니다.");

} else if ((user == 1) && (com == 3)){

System.out.println("컴퓨터는 보를 냈습니다.");

System.out.println("당신이 이겼습니다.");

} else if ((user == 2) && (com == 1)){

System.out.println("컴퓨터는 가위를 냈습니다.");

System.out.println("당신이 이겼습니다.");

} else if ((user == 2) && (com == 3)){

System.out.println("컴퓨터는 보를 냈습니다.");

System.out.println("당신이 졌습니다.");

} else if ((user == 3) && (com == 1)){

System.out.println("컴퓨터는 가위를 냈습니다.");

System.out.println("당신이 졌습니다.");

} else {

System.out.println("컴퓨터는 보를 냈습니다.");

System.out.println("당신이 이겼습니다.");

}

sc.close();


} //end main()


} // end class GameMain02 



'개발자의 길 > JAVA' 카테고리의 다른 글

JAVA CLASS : 배열  (0) 2016.12.29
JAVA CLASS : 반복문(for, while, loop)  (0) 2016.12.29
JAVA CLASS : 연산자  (0) 2016.12.29
JAVA CLASS : 데이터 타입  (0) 2016.12.29
이클립스(eclipse) 설치  (0) 2016.12.29
Comments