제어문
흐름을 조건에 따라 통제하는 명령
조건문
if
문장의 실행 여부를 결정한다.
조건식이 참이면 종속 문장을 실행 거짓이면 다음 문장을 실행
if(조건식){}
중괄호는 안의 문장이 한줄일때 생략 가능하다.
조건식 안에는 문자열은 올 수 없다
ex) if(“true”){ System.out.print(“조건식에 문자열이 왔기에 성립되지 않는다.”);}
if ~ else
if는 필수로 들어간다.
else를 썼을 때 조건을 한번 안보는 차이점이 있다.
if ~ else if ~ else
다중 if문
else if를 사용할 때 순서를 신경 쓴다
if 문에서 조건을 충족했을 경우 if else문은 보지 않기 때문이다.
작은 범위부터 큰 범위의 순서로 실행해야 한다.
조건문에 문자 넣기.
Scanner sc = new Scanner(System.in);
System.out.println("학점을 입력하세요(A~F)");
char grade = sc.next().toUpperCase().charAt(0);
if(grade == 'A' || grade =='B') {
System.out.println("참 잘했어요");
}
else if(grade == 'C' || grade =='D') {
System.out.println("분발합시다");
}
char grade =sc.next(). toUpperCase(). charAt(0);
이 줄을↓
String tmp = sc.next();
tmp = tmp.toUpperCase();
char grade = tmp.charAt(0);
풀어쓸 수 있다
Scanner sc = new Scanner(System.in);
System.out.println("학점을 입력하세요(A~F)");
String grade = sc.next();
if(grade.equalsIgnoreCase("A") || grade.equalsIgnoreCase("B"))
System.out.println("참 잘했어요");
else if(grade.equalsIgnoreCase("C") || grade.equalsIgnoreCase("D"))
System.out.println("분발합시다");
문자열은 관계 연산자를 쓸 수 없다. 그렇기에 메서드를 써야 한다.
equalsIgnoreCase 대소문자를 무시하고 괄호 안과 같다면 true
예제
-세 수를 입력받아 큰 수 출력하기
-두 수를 입력받아 큰 수가 짝수일 때 출력하기
-수를 입력받아 짝수인 동시에 3의 배수인 수를 출력
package conditions;
import java.util.Scanner;
public class Quiz2 {
public static void main(String[] args) {
int data1,data2,data3,max,sum;
//세 수를 입력받아 큰수 출력
Scanner sc = new Scanner(System.in);
System.out.println("첫번째 수를 입력하세요");
data1 = sc.nextInt();
System.out.println("두번째 수를 입력하세요");
data2 = sc.nextInt();
System.out.println("세번째 수를 입력하세요");
data3 = sc.nextInt();
max=data1;
if(max < data2) {
max = data2;
}
if(max < data3) {
max = data3;
}
System.out.println("제일 큰 수는"+max+"입니다");
//두 수를 입력받아 큰 수가 짝수일때 출력
System.out.println("첫번째 수를 입력하세요");
data1 = sc.nextInt();
System.out.println("두번째 수를 입력하세요");
data2 = sc.nextInt();
max=data1;
if(max < data2) {max=data2;}
if(max%2==0)System.out.println("큰 수이며 짝수는"+max+"입니다");
else System.out.println("큰 수이며 짝수는없습니다");
//두 수를 입력받아 합이 짝수이고 3의 배수인 수를 출력
System.out.println("첫번째 수를 입력하세요");
data1 = sc.nextInt();
System.out.println("두번째 수를 입력하세요");
data2 = sc.nextInt();
//이때 2의배수 동시에 3의 배수이므로 2,3의 공배수인 6의 배수로 작성할 수도 있다.
sum= data1 + data2;
if(sum%2==0 && sum%3==0) {
System.out.println(sum+"은짝수이며 3의배수입니다.");
}
else System.out.println(sum+"은짝수나3의배수가 아닙니다.");
sc.close();
}
}
세 수를 입력받아 최댓값 최솟값 구하기
package conditions;
import java.util.Scanner;
public class Quiz4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int data1,data2,data3,max,min;
System.out.println("첫번째수를 입력 : ");
data1 = sc.nextInt();
System.out.println("두번째수를 입력 : ");
data2 = sc.nextInt();
System.out.println("세번째수를 입력 : ");
data3 = sc.nextInt();
max = data1;
min = data2;
if(max < data2) {
max = data2;
min = data1;
}
if(max<data3) {
max = data3;
}
else if (data3<min) min = data3;
System.out.println("큰수는 : "+ max+"작은수는 : "+min);
}
}
학점을 입력받아 문자열 출력하기
a.read() 메서드를 사용함
b.equalsIgnoreCase()를 사용함
package conditions;
import java.io.IOException;
import java.util.Scanner;
public class Quiz5 {
public static void main(String[] args)throws IOException {
Scanner sc = new Scanner(System.in);
char score;
int year;
String message;
//read()메소드를 사용함
System.out.println("학점을 입력하세요(A~F)");
score = (char) System.in.read();
if(score == 'A' || score =='B') {
System.out.println("참 잘했어요");
}
else if(score == 'C' || score =='D') {
System.out.println("분발합시다");
}
//equalsIgnoreCase()를 사용함
System.out.println("학점을 입력하세요(A~F)");
String grade = sc.next();
if(grade.equalsIgnoreCase("A")||grade.equalsIgnoreCase("B"))
System.out.println("참 잘했어요");
else if(grade.equalsIgnoreCase("C")||grade.equalsIgnoreCase("D"))
System.out.println("분발합시다");
sc.close();
}
}
커피 수를 입력받아 금액 출력
커피의 개당 가격은 2000원이다. 10개 초과하면 초과하는 양에 대해서만 1500원
과자 수를 입력받아 금액 출력
과자 1개의 1000원 10개 이상 사면 전체 금액에10%할인 100개이상사면 전체금액에 12% 할인
package conditions;
import java.util.Scanner;
public class Quiz6 {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
int coffee,snack,cofTot,snaTot;
//커피의 개당 가격은 2000원이다. 10개 초과하면 초과하는 양에 대해서만 1500원
//커피의 개수를 입력받아 금액출력
System.out.println("커피를 몇개 사실래요?");
coffee = sc.nextInt();
cofTot = 2000 *coffee;
if(coffee>10) {cofTot = cofTot-((coffee-10)*500);}
System.out.println("커피"+coffee+"개의 가격은"+cofTot+"원입니다");
//과자 1개의 1000원 10개이상 사면 전체금액에10%할인 100개이상사면 전체금액에 12%할인
//개수를 입력받아 금액 계산하기
System.out.println("과자를 몇개 사실래요?");
snack = sc.nextInt();
snaTot = snack*1000;
if(snack>=100) {
snaTot = snaTot*88/100;
}else if(snack>=10) {
snaTot = snaTot*90/100;
}System.out.println("과자"+snack+"개의 가격은"+snaTot+"원입니다");
sc.close();
}
}
'코딩 공부 > Java' 카테고리의 다른 글
자바 콘솔 입 출력 (0) | 2022.08.16 |
---|---|
Java switch case와 반복문 for (0) | 2022.08.15 |
연산자 (0) | 2022.08.13 |
기본 개념 (0) | 2022.08.12 |
Eclipse 환경설정 (0) | 2022.07.08 |