我只想知道是否有任何方法阻止用戶輸入負數或4位以上的數字。反正也有把這個邏輯放到一個GUI中的嗎?這裏是我的代碼:如何防止輸入負數或4位以上的數字
import java.util.Scanner;
public class EasterSunday
{
public static void main(String[] args)
{
int year; // declarations
Scanner input = new Scanner(System.in); //start Scanner
System.out.println("Enter in a year to find out the day and month of that Easter Sunday.");
year = input.nextInt();
int a = year%19;
int b = year%4;
int c = year%7;
int d = (19 * a + 24) %30;
int e = (2 * b + 4 * c + 6 * d + 5) %7;
int eSunday = (22 + d + e);
if ((year >= 1900) && (year <= 2099) && (year != 1954) && (year != 1981) && (year != 2049) && (year != 2076))
{
if (eSunday <= 30)
System.out.println("Easter Sunday in " + year + " is March, " + eSunday);
else
System.out.println("Easter Sunday in " + year + " is April, " + (eSunday - 30));
}
else
{
if (eSunday <= 30)
System.out.println("Easter Sunday in " + year + " is March, " + (eSunday - 7));
else
System.out.println("Easter Sunday in " + year + " is April, " + (eSunday - 37));
}
}
}
你應該問一個問題,並縮小代碼? –
對不起,這個我知道下次:) – FluX