英文:
How can I ask for an input again instead of just having an error
问题
public class Mystuff {
public static void main(String[] args) {
System.out.println("请输入一个字符串:");
String s = SubProgram.string();
System.out.println("输入的字符串为:" + s);
int i = SubProgram.getInt();
System.out.println("输入的整数为:" + i);
}
}
类:
import java.util.Scanner;
public class SubProgram {
private static final Scanner INPUT = new Scanner(System.in);
public static String string() {
String s = INPUT.nextLine();
return s;
}
public static int getInt() {
System.out.println("请输入一个数字:");
while (!INPUT.hasNextInt()) {
System.out.println("输入错误,请重新输入数字:");
INPUT.next(); // 清除错误输入
}
int num = INPUT.nextInt();
return num;
}
}
英文:
So I want to ask the user for an input. The thing is that I also want the program to give an error message and ask for an input again if the user, instead of a number, try and type something that's not a number.
public class Mystuff {
public static void main(String[] args) {
System.out.println("Write your string");
String s = SubProgram.string();
System.out.println("The string: " +s);
int i = SubProgram.getInt();
System.out.println("The int: " +i);
}
}
Classes:
import java.util.Scanner;
public class SubProgram {
private static final Scanner INPUT = new Scanner(System.in);
public static String string() {
String s = INPUT.nextLine();
return s;
}
public static int getInt(){
System.out.println("Write your number");
int num = INPUT.nextInt();
return num;
}
}
答案1
得分: 0
为了重复执行某个操作,可以使用循环。
基本的模式是:
do {
打印提示信息;
读取输入;
验证输入;
如果无效,打印错误消息;
} while (尚未提供有效输入);
一个合理的方法是在“验证输入”步骤中设置一个标志(比如说,valid
)为真或假,然后循环是 while (!valid)
。
对于使用 nextInt()
,如果输入不是有效的整数,它将抛出异常。你需要捕捉那个异常(参见 try-catch
语句)并使用它来设置“无效”。
do {
try {
..blah blah(省略部分)
n = scanner.nextInt();
valid = true;
}
catch (NumberFormatException ex) {
在这里打印错误;
valid = false;
}
} while (!valid);
我相信有了这些解决方案的概要,你可以编写实际的代码。
英文:
To repeat something, use a loop.
The basic pattern is:
do {
print a prompt;
read input;
validate input;
if not valid, print error message;
} while (valid input has not been provided);
A reasonable approach is to set some flag (say, valid
) true or false in the "validate input" step, and then the loop is while (!valid)
.
For use of nextInt()
, it will throw an exception if the input is not a valid integer. You'll need to catch that exception (see try-catch
statement) and use that to set "not valid".
do {
try {
..blah blah
n = scanner.nextInt();
valid = true;
}
catch (NumberFormatException ex) {
print error here;
valid = false;
}
} while (!valid);
I trust that, with these sketches of a solution, you can write the actual code.
答案2
得分: 0
如您已被告知,请使用循环,例如:
public static int getInt(){
String num = null;
while (num == null) {
System.out.println("请输入您的数字");
num = INPUT.nextLine();
if (!num.matches("\\d+")) {
System.err.println("提供的数字无效! (" + num + ")");
num = null;
}
}
return Integer.valueOf(num);
}
英文:
As you have already been informed, use a loop, for example:
public static int getInt(){
String num = null;
while (num == null) {
System.out.println("Write your number");
num = INPUT.nextLine();
if (!num.matches("\\d+")) {
System.err.println("Invalid number supplied! (" + num + ")");
num = null;
}
}
return Integer.valueOf(num);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论