如果输入正确的整数值之一,我的程序正常工作,输出正确 。我不确定如何解决它,就像我输入另一个值我只会得到“无效输入”(我放在我的代码中),但月份仍将是错误输入的整数。我需要知道如何在我的程序中解决这个问题。
import java.util.Scanner;
public class whichMonth {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Please enter the month name or number: ");
int monthNumber = input.nextInt();
// set up variable access to the class
AnyMonth inputMonthNumber = new AnyMonth();
// set the month number from user input
inputMonthNumber.setMonthnum(monthNumber);
// get the month name from user input number
String monthName = inputMonthNumber.getMonthName(monthNumber);
// print out the month number and month name
System.out.println("The month number is: " + inputMonthNumber.getMonthnum(monthNumber)
+ "\nThe month name is: " + inputMonthNumber.getMonthName(monthNumber));
}
}
class AnyMonth {
int Monthnum;
String monthName;
public AnyMonth() {
Monthnum = 1;
}
public AnyMonth(int currentMonthNumber) {
Monthnum = currentMonthNumber;
switch(currentMonthNumber) {
case 1:
Monthnum = 1;
monthName = "January";
System.out.println("January");
break;
case 2:
Monthnum = 2;
monthName = "February";
System.out.println("February");
break;
case 3:
Monthnum = 3;
monthName = "March";
System.out.println("March");
break;
case 4:
Monthnum = 4;
monthName = "April";
System.out.println("April");
break;
case 5:
Monthnum = 5;
monthName = "May";
System.out.println("May");
break;
case 6:
Monthnum = 6;
monthName = "June";
System.out.println("June");
break;
case 7:
Monthnum = 7;
monthName = "July";
System.out.println("July");
break;
case 8:
Monthnum = 8;
monthName = "August";
System.out.println("August");
break;
case 9:
Monthnum = 9;
monthName = "September";
System.out.println("September");
break;
case 10:
Monthnum = 10;
monthName = "October";
System.out.println("October");
break;
case 11:
Monthnum = 11;
monthName = "November";
System.out.println("November");
break;
case 12:
Monthnum = 12;
monthName = "December";
System.out.println("December");
break;
default:
Monthnum = 1;
monthName = "January";
}
}
public AnyMonth(String userMonthName) {
switch(userMonthName) {
case "January":
Monthnum = 1;
monthName = "January";
System.out.println("January");
break;
case "February":
Monthnum = 2;
monthName = "February";
System.out.println("February");
break;
case "March":
Monthnum = 3;
monthName = "March";
System.out.println("March");
break;
case "April":
Monthnum = 4;
monthName = "April";
System.out.println("April");
break;
case "May":
Monthnum = 5;
monthName = "May";
System.out.println("May");
break;
case "June":
Monthnum = 6;
monthName = "June";
System.out.println("June");
break;
case "July":
Monthnum = 7;
monthName = "July";
System.out.println("July");
break;
case "August":
Monthnum = 8;
monthName = "August";
System.out.println("August");
break;
case "September":
Monthnum = 9;
monthName = "September";
System.out.println("September");
break;
case "October":
Monthnum = 10;
monthName = "October";
System.out.println("October");
break;
case "November":
Monthnum = 11;
monthName = "November";
System.out.println("November");
break;
case "December":
Monthnum = 12;
monthName = "December";
System.out.println("December");
break;
default:
Monthnum = 1;
monthName = "January";
}
}
public void setMonthnum (int userMonth) {
Monthnum = userMonth;
if (userMonth < 1 || userMonth > 12){
System.out.println("Invalid input");
this.Monthnum = userMonth;
}
}
public int getMonthnum(int currentMonthNumber) {
Monthnum = currentMonthNumber;
return currentMonthNumber;
}
public String getMonthName(int currentMonthName) {
Monthnum = currentMonthName;
// use swtich-case here instead, default case should return 1/Jan
switch(currentMonthName) {
case 1:
monthName = "January";
break;
case 2:
monthName = "February";;
break;
case 3:
monthName = "March";
break;
case 4:
monthName = "April";
break;
case 5:
monthName = "May";
break;
case 6:
monthName = "June";
break;
case 7:
monthName = "July";
break;
case 8:
monthName = "August";
break;
case 9:
monthName = "September";
break;
case 10:
monthName = "October";
break;
case 11:
monthName = "November";
break;
case 12:
monthName = "December";
break;
default:
monthName = "January";
}
return monthName;
}
public String toString(String monthNameToString){
monthName = monthNameToString;
return monthNameToString;
}
public boolean equals(int month) {
if (Monthnum == month) {
return true;
}
else {
return false;
}
}
public boolean greaterThan(int month) {
if (Monthnum > month) {
return true;
}
else {
return false;
}
}
public boolean lessThan(int month) {
if (Monthnum < month) {
return true;
}
else {
return false;
}
}
}
答案 0 :(得分:2)
首先,由于使用
,您的代码只会占用一个intnextInt();
正在使用(如VVN所述)。
int monthNumber = input.nextInt();
您的输出要求提供月份名称或号码,但您的程序只会接受一个号码。
接下来,您的程序默认为1月份和错误号码的原因是因为您处理错误案例的方式。
在setMonthnum()方法中,您仍在设置Monthnum变量。
if (userMonth < 1 || userMonth > 12){
System.out.println("Invalid input");
this.Monthnum = userMonth;
}
因此,您的程序将使用输入的任何不正确的int,并将继续执行。 1月开始显示为代码行
String monthName = inputMonthNumber.getMonthName(monthNumber);
正在通过switch语句查找错误的数字/输入。它没有找到匹配项,并使用默认情况1月。
default:
monthName = "January";
如果你想解决这个问题,你应该添加某种形式的输入陷阱来提示用户输入另一个数字/输入,因为它们之前的数字/输入无效。如果您不希望提示输入其他内容,只需结束执行:
System.exit(0);
将停止执行而不会出现任何错误。但是,我强烈建议添加某种形式的输入陷阱来管理它。此外,如果您想在初始输入中实际接受月份名称,则可能需要使用
nextLine();
over nextInt。这将允许您将输入行作为字符串接收。从那里,您可以确定输入是月份的数字还是名称,并相应地采取行动(parseInt /使用月份名称)。