天天看點

提示客戶輸入一個月份,必須在1~12月之間,如果輸入錯誤就讓客戶重新輸入,然後根據月份輸出季節,3~5為春天,6~8為夏天,9~11月為秋天,12~2為冬天。

import java.util.Scanner;

public class WhileMonth {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int month;

boolean isRight;

do{

System.out.println(“請輸入月份(1-12月):”);

month = input.nextInt();

switch (month) {

case 3:

case 4:

case 5:

System.out.println(“現在是春天”);

isRight=true;

break;

case 6:

case 7:

case 8:

System.out.println(“現在是夏天”);

isRight=true;

break;

case 9:

case 10:

case 11:

System.out.println(“現在是秋天”);

isRight=true;

break;

case 12:

case 1:

case 2:

System.out.println(“現在是冬天”);

isRight=true;

break;

default:

System.out.println(“輸入錯誤,請重新輸入!”);

isRight=false;

break;

}

}while(isRight==false);

System.out.println(“程式結束!”);