import java.util.Scanner;
public class SuccessOrFail {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("점수를 입력하시오: ");
int score = scanner.nextInt();
if (score >= 80)
System.out.println("합격입니다");
scanner.close();
}
}
import java.util.Scanner;
public class MultipleOfThree {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("수를 입력하시오: ");
int number = scanner.nextInt();
if (number % 3 == 0)
System.out.println("3의 배수입니다.");
else
System.out.println("3의 배수가 아닙니다.");
scanner.close();
}
}
import java.util.Scanner;
public class NestedIf {
public static void main(String[] args) {
int score = 0;
char grade = ' ', opt = '0';
System.out.printf("점수를 입력하세요>");
Scanner scanner = new Scanner(System.in);
score = scanner.nextInt();
System.out.printf("당신의 점수는 %d입니다%n", score);
if (score >= 90) { //90이상
grade = 'A';
if (score >= 98) { //90이상 98이하..
opt = '+';
} else if (score < 94) {
opt = '-';
}
} else if (score >= 80) {
grade = 'B';
if (score >= 88) {
opt = '+';
} else if (score < 84) {
opt = '-';
}
} else {
grade = 'C';
}
System.out.printf("당신의 학점은 %c%c입니다%n", grade, opt);
}
}
import java.util.Scanner;
public class Grading {
public static void main(String[] args) {
char grade;
Scanner scanner = new Scanner(System.in);
System.out.printf("점수를 입력하세요(0-100): ");
int score = scanner.nextInt();
if(score>=90)
grade = 'A';
else if(score>=80) //80이상 90미만
grade = 'B';
else if(score>=70)
grade = 'C';
else if(score>=60)
grade = 'D';
else
grade = 'F';
System.out.println("학점은 " + grade + "입니다.");
scanner.close();
}
}
import java.util.Scanner;
public class Grading {
public static void main(String[] args) {
char grade;
Scanner scanner = new Scanner(System.in);
System.out.printf("점수를 입력하세요(0-100): ");
int score = scanner.nextInt();
switch (score / 10) {
case 10:
case 9:
grade = 'A';
break;
case 8:
grade = 'B';
break;
case 7:
grade = 'C';
break;
case 6:
grade = 'D';
break;
default:
grade = 'F';
}
System.out.println("학점은 " + grade);
}
}
import java.util.Scanner;
public class Month {
public static void main(String[] args) {
System.out.printf("현재 월을 입력하세요: ");
Scanner scanner = new Scanner(System.in);
int month = scanner.nextInt();
switch (month){
case 3:
case 4:
case 5:
System.out.printf("봄!");
break;
case 6:
case 7:
case 8:
System.out.println("여름");
case 9:
case 10:
case 11:
System.out.println("가을");
case 12:
case 1:
case 2:
System.out.println("겨울");
}
}
}
for문의 형태
for(초기문; 조건식; 반복 후 작업){
작업문
}
for(초기문; true; 반복 후 작업){ //무한반복
작업문
}
for(초기문; ; 반복 후 작업){ //무한반복
작업문
}
for(i=0; i<10; i++, System.out.println(i)){
작업문
}
for(int i = 0; j= 100; i<=50 && j>=50; i++,j--){
작업문
}
바깥의 for문이 한번 실행될 때 마다 중첩된 for문이 반복해서 돌고, 다시 바깥 for문으로 돌아간다.
public class ForFor {
public static void main(String[] args) {
for(int x = 2; x<=9; x++){
for(int y = 1; y<=9; y++){
System.out.print(x + "x" + y + "=" + (x*y) + ",");
}
}
}
}
while문의 형태
while(조건식){
작업문
}
public class WhileSum {
public static void main(String[] args) {
int sum = 0;
int i = 1;
while (i <= 100) {
sum += i;
i++;
}
System.out.println(sum);
}
}
우선 실행하고 실행 결과에 따라 반복 실행을 계속 할지 결정
do{
작업문;
}
while(조건식);
public class DoWhile {
public static void main(String[] args) {
char c = 'a';
do {
System.out.println(c);
c = (char) (c + 1);
}
while (c <= 'z');
}
}
반복문을 빠져나가지 않으면서 다음 반복으로 진행
public class ContinueEx {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 2 != 0)
continue;
System.out.println(i);
}
}
}
output
2
4
6
8
10
public class BreakExample {
public static void main(String[] args) {
while (true){
int num = (int)(Math.random()*6) + 1;
System.out.println(num);
if(num == 6){
break;
}
}
System.out.println("종료");
}
}
public class BreakExample {
public static void main(String[] args) {
Outter:
for (char upper = 'A'; upper <= 'Z'; upper++) {
for (char lower = 'a'; lower <= 'z'; lower++) {
System.out.println(upper + "-" + lower);
if (lower == 'g') {
break Outter;
}
}
}
System.out.println("종료");
}
}
public class BreakExample {
public static void main(String[] args) {
Loop1:
for (int i = 2; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
if (j == 5)
break Loop1;
// break;
System.out.println(i + "*" + j + "=" + i * j);
}
System.out.println();
}
}
}