본문 바로가기

프로그램공부/java공부

while이용한 구구단 반복

package test;

import java.util.Scanner;

public class Calendar {
private static void prom() {
String prompt = "cal>";
System.out.println("월을 입력하세요\n"+prompt);

}
public static void main(String[] args) {
    int[] date = {31,29,31,30,31,30,31,31,30,31,30,31};
    prom();
    Scanner scanner = new Scanner(System.in);
    int month = 1;
    while(true) {
        month = scanner.nextInt();
        if(month==-1) {
            break;}
        if (month>12) {
            prom();
            continue;
        }
        System.out.printf("%d월은 %d일까지 있습니다.\n",month,date[month-1]);
        prom();

    }
    System.out.println("bye");
    scanner.close();


}

}```
package test;

import java.util.Scanner;

public class calendar {

public static void main(String[] args) {

    int[] date = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int month = 1;
    System.out.println("월을 입력하세요");
    Scanner scanner = new Scanner(System.in);
    while (true) {
        month = scanner.nextInt();
        if(month ==-1)
            break;
        System.out.printf("%d의 일은 %d 입니다.", month, date[month - 1]);
    }
    System.out.println("bye");
    scanner.close();
}

}

```