所以在我的Java课程中,我正在使用eclipse霓虹灯代码来显示根据年份和月份在一个月内的天数。我有所有写的代码,但如果你输入一个数字,然后1-12一个月,我需要一个错误信息弹出,我不知道如何弹出一个输出而不是另一个。以下是我的代码:
import java.util.Scanner;
public class Month
{
public static void main(String[] args)
{
// Create a Scanner object to obtain information from user
Scanner input = new Scanner (System.in);
int MonthNum; // To hold the month from user input
int Year; // To hold the year
int numDays;
System.out.print("Please enter a month, 1-12:");
MonthNum = input.nextInt();
System.out.print("Please enter year:");
Year = input.nextInt();
if (MonthNum == 2)
{
if( (Year % 4 == 0) && (Year % 400 == 0) && !(Year % 100 == 0) )
numDays= 29;
else
numDays= 28;
}
else if (MonthNum == 1 || MonthNum == 3 || MonthNum == 5 || MonthNum == 7 || MonthNum == 8 || MonthNum == 10 || MonthNum == 12)
numDays = 31;
else
numDays = 30;
String Monthnam = null;
if (MonthNum == 1)
Monthnam = "January";
else if (MonthNum == 2)
Monthnam = "Febuary";
else if (MonthNum == 3)
Monthnam = "March";
else if (MonthNum == 4)
Monthnam = "April";
else if (MonthNum == 5)
Monthnam = "May";
else if (MonthNum == 6)
Monthnam = "June";
else if (MonthNum == 7)
Monthnam = "July";
else if (MonthNum == 8)
Monthnam = "August";
else if (MonthNum == 9)
Monthnam = "September";
else if (MonthNum == 10)
Monthnam = "October";
else if (MonthNum == 11)
Monthnam = "November";
else if (MonthNum == 12)
Monthnam = "December";
System.out.print(Monthnam + " " + Year + " " + "has" + " " + numDays + " " + "days");
}
}
答案 0 :(得分:0)
第13行之后你会有一个很大的if / else块
if( MonthNum <1 || MonthNum > 12 ){
System.out.println("Invalid Month: " + MonthNum)
}
else {
... the rest of your code
}
如果你想提示然后使用while循环
boolean haveInput = false;
while( !haveInput ){
// get the MonthNum
if( MonthNum <1 || MonthNum > 12 ){
System.out.println("Invalid Month: " + MonthNum)
}
else {
haveInput = true;
}
}
// now the rest of your code
答案 1 :(得分:0)
import java.util.Scanner;
public class Month
{
public static void main(String[] args){
// Create a Scanner object to obtain information from user
Scanner input = new Scanner (System.in);
int MonthNum; // To hold the month from user input
int Year; // To hold the year
int numDays;
if( MonthNum < 1 || MonthNum > 12 ){
System.out.println("Invalid Month: " + MonthNum);
}
else {
System.out.print("Please enter a month, 1-12:");
MonthNum = input.nextInt();
System.out.print("Please enter year:");
Year = input.nextInt();
if (MonthNum == 2)
{
if( (Year % 4 == 0) && (Year % 400 == 0) && !(Year % 100 == 0) )
numDays= 29;
else
numDays= 28;
}
else if (MonthNum == 1 || MonthNum == 3 || MonthNum == 5 || MonthNum == 7 || MonthNum == 8 || MonthNum == 10 || MonthNum == 12)
numDays = 31;
else
numDays = 30;
String Monthnam = null;
if (MonthNum == 1)
Monthnam = "January";
else if (MonthNum == 2)
Monthnam = "Febuary";
else if (MonthNum == 3)
Monthnam = "March";
else if (MonthNum == 4)
Monthnam = "April";
else if (MonthNum == 5)
Monthnam = "May";
else if (MonthNum == 6)
Monthnam = "June";
else if (MonthNum == 7)
Monthnam = "July";
else if (MonthNum == 8)
Monthnam = "August";
else if (MonthNum == 9)
Monthnam = "September";
else if (MonthNum == 10)
Monthnam = "October";
else if (MonthNum == 11)
Monthnam = "November";
else if (MonthNum == 12)
Monthnam = "December";
System.out.print(Monthnam + " " + Year + " " + "has" + " " + numDays + " " + "days");
}
}}