," and then the user must enter a number. I-6ren">
gpt4 book ai didi

java - 日期/月份验证

转载 作者:行者123 更新时间:2023-12-02 04:22:00 25 4
gpt4 key购买 nike

这是我想要做的事情的描述:

First, the program will print "Enter month>," and then the user must enter a number. If the entered number is not in the range of 1 to 12 as the following error message is printed "Improper month.", And the program then ends. If the entered month is correct, the program will print "Enter the day of month>," and then the user must enter a to-speech. If the number does not correspond to a day in the previous input the month, the following error message is printed "Wrong day of the month.", And the program then ends. If both the input month number and the entered day of the month is correct, then the program should print "The correct date.", And then terminated.

但是我在下面收到此错误,但我不明白为什么

Exception in thread "main" java.lang.NumberFormatException: For input string: "java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\ ][decimal separator=\,][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q∞\E]"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.parseInt(Integer.java:499)
at upp.main(upp.java:10)


import java.util.Scanner;
public class upp {
public static void main(String[] args) {

int[] numberOfDaysEachMonth = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
System.out.println("Enter month > ");
Scanner month = new Scanner(System.in);
int userInputMonth = Integer.parseInt(month.toString());
if (userInputMonth > 0 && userInputMonth < 13) {
System.out.println("Enter the day of month > ");
Scanner day = new Scanner(System.in);
int userInputDay = Integer.parseInt(day.toString());
if (userInputDay > 0 && userInputDay < numberOfDaysEachMonth[userInputMonth - 1]) {
System.out.println("Correct date.");
} else {
System.out.println("Wrong date.");
}
} else {
System.out.println("Wrong month.");
}
}
}

最佳答案

 int userInputMonth = Integer.parseInt(month.toString());

它应该是以下语句之一:

// Read as string, convert to int.
int userInputMonth = Integer.parseInt(month.next());

// Read as int using nextInt()
int userInputMonth = month.nextInt();

<小时/>
Scanner day = new Scanner(System.in);

创建 Scanner 的对象后,您无需再次创建它。使用同一对象从 System.in 读取进一步的输入。它应该是以下语句之一。

// Read as string, convert to int.
int userInputDay = Integer.parseInt(month.next());

// Read as int using nextInt()
int userInputDay = month.nextInt();
<小时/>

您还需要使用<=当天检查的状况而不是 < .

if (userInputDay > 0 && userInputDay <= numberOfDaysEachMonth[userInputMonth - 1])

PS:重命名 Scannerscanner这样的对象或read然后用它来读取

<小时/>
import java.util.Scanner;

public class upp {
public static void main(String[] args) {

int[] numberOfDaysEachMonth = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
System.out.println("Enter month > ");
Scanner scanner = new Scanner(System.in);
int userInputMonth = Integer.parseInt(scanner.next());
if (userInputMonth > 0 && userInputMonth < 13) {
System.out.println("Enter the day of month > ");

int userInputDay = Integer.parseInt(scanner.next());
if (userInputDay > 0 && userInputDay <= numberOfDaysEachMonth[userInputMonth - 1]) {
System.out.println("Correct date.");
} else {
System.out.println("Wrong date.");
}
} else {
System.out.println("Wrong month.");
}
}
}

关于java - 日期/月份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32671946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com