My footsteps
if 조건식,if-else문,else if,switch문 본문
728x90
public class Restart {
public static void main(String[] args) {
int x = 0;
// System.out.printf("x=%d일때, 참인것은%n",x);
//%d=정수 출력,%n=줄바꿈
if(x==0) System.out.println("x==0");//x는 0이다(참) ▶출력
if(x!=0) System.out.println("x!==0");//x는 0이 아니다(거짓)
if(!(x==0)) System.out.println("!(x==0)");//x는 0이 아니다(거짓)
if(!(x!=0)) System.out.println("!(x!==0)");//x는 0이다(이중부정=참) ▶출력
//참인것만 출력됨
x = 1;
if(x==0) System.out.println("x==0");//x는 0이다(거짓)
if(x!=0) System.out.println("x!==0");//x는 0이 아니다(참) ▶출력
if(!(x==0)) System.out.println("!(x==0)");//x는 0이 아니다(참) ▶출력
if(!(x!=0)) System.out.println("!(x!==0)");//x는 0이다(이중부정=거짓)
}
}
scanner문과 결합
import java.util.Scanner;
public class Restart {
public static void main(String[] args) {
System.out.println("숫자 입력해잉");
Scanner Scanner = new Scanner(System.in);
int input = Scanner.nextInt();
if(input ==0) {
System.out.println("제대로 입력햇구마");
}
else {
System.out.println("떼잉 그거아니야");
}
}
}
else if문
import java.util.Scanner;
public class Restart {
public static void main(String[] args) {
int score = 0;
char grade = ' ';
System.out.println("점수를 입력하세용");
Scanner Scanner = new Scanner(System.in);
score = Scanner.nextInt();
if(score >= 90) { //만약 점수가 90점 이상이라면
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C';
} else {
grade = 'F';
}
System.out.println("당신의 점수는 "+grade+" 다!");
}
}
//여기서 else문 없애려면 char grade='D'로 설정해놓으면 된다.
중첩 else if문
import java.util.Scanner;
public class Restart {
public static void main(String[] args) {
int score = 0;
char grade = 'C', opt = '0'; //opt는 +,-를 붙일 문자 변수
System.out.println("점수를 입력하세용");
Scanner Scanner = new Scanner(System.in);
score = Scanner.nextInt();
if(score >= 90) { //만약 점수가 90점 이상이라면
grade = 'A';
if (score >= 98) {
opt = '+';
} else if(score <= 94) {
opt = '-';
}
} //대빵 if문은 이렇게 중첩 if문과 else if문들을 다 감싸줘야함
else if(score >= 80) {
grade = 'B';
if(score >= 88) {
opt = '+';
} else if(score <= 84) {
opt = '-';
}
} //대빵 else if도 마찬가지. 안에가 중첩이면 다 감싸줘야함
System.out.printf("당신의 학점은 %c%c입니다.%n",grade,opt);
} //%C = 문자
}
switch문
import java.util.Scanner;
public class Restart {
public static void main(String[] args) {
System.out.println("현재 월을 입력해랏");
Scanner Scanner = new Scanner(System.in);
int month = Scanner.nextInt();
switch(month) { //조건식
case 3:
case 4:
case 5://케이스문
System.out.println("봄이다..");
break;
case 6: case 7: case 8:
System.out.println("여름이다..");
break;
case 9: case 10: case 11:
System.out.println("가을이다..");
break;
default:
System.out.println("겨울이다..");
}
}
}
728x90
'예습 > code' 카테고리의 다른 글
While문,break문,continue문 (0) | 2023.01.15 |
---|---|
Math.random,for문 (0) | 2023.01.15 |
인터페이스 사용 이유 (0) | 2023.01.14 |
인터페이스의 다형성 (0) | 2023.01.14 |
추상클래스 (0) | 2023.01.14 |