Java扫描仪和方法的异常问题
Java Exception problem with scanner and method
我在 java 中遇到了一些问题,在 while 循环中将扫描器放在输出之后。
扫描仪扫描要走的方法,然后返回到循环的开始
重置变量。
我已经试过了,但没有找到任何可以理解的解决方案(我是 java 的新手,这对我来说很难),
或者自己解决。
这是完整的代码(我知道代码效率不高):
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int con = 1;
System.out.println("Hey!,welcome to my games!");
Scanner scanner = new Scanner(System.in);
String game;
while (con == 1) {
System.out.println(
"Here Some games i have(Enter the text for the one you want):\nto stop=0\n1)calculator=1\n2)!Soon! Random numbers");
game = scanner.nextLine();
calculator();
scanner.reset();
System.out.println(game);
// if(game.equals("0")) {
// con=0;
// }
// else if(game.equals("1")) {
// System.out.println("Welcome Back!");
// }
// else {
// System.out.println("There is a mistake in your text");
// }
}
scanner.close();
}
static void calculator() {
int num1, num2, con = 1, stop = 1;
String op, ad = "add", su = "sub", mul = "multi", di = "div";
Scanner af = new Scanner(System.in);
while (con == 1) {
stop = 1;
System.out.println("Write number 1");
num1 = af.nextInt();
System.out.println("Write number 2");
num2 = af.nextInt();
System.out.println(
"Write an operation (one of these):\nAddition=add\nSubtraction=sub\nMultiplication=multi\nDivision=div");
op = af.next();
op = op.toLowerCase();
int minus = num1 - num2;
int plus = num1 + num2;
if (op.equals(ad)) {
System.out.println("The Operation is:Addition\n" + num1 + "+" + num2 + "=" + plus);
} else if (op.equals(su)) {
System.out.println("The Operation is:Subtraction\n" + num1 + "-" + num2 + "=" + minus);
} else if (op.equals(mul)) {
System.out.println("The Operation is:Multiplication\n" + num1 + "*" + num2 + "=" + num1 * num2);
} else if (op.equals(di)) {
System.out.println("The Operation is:Division\n" + num1 + "/" + num2 + "=" + num1 / num2);
} else {
System.out.println("Um,Did you make a mistake in your text?\nDo you want the calculator again?");
String yn = af.next();
yn = yn.toLowerCase();
if (yn.equals("yes") || yn.equals("yep")) {
stop = 0;
}
}
if (stop == 1) {
con = 0;
}
}
af.close();
}
}
如您所见,我尝试自己解决它,甚至对一些代码发表了评论,
但是当它运行到该方法并返回时,它失败了,因为扫描器认为有
在我写东西之前要扫描的东西。这是例外-
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at Main.main(Main.java:12)
你记得导入 java.util.Scanner 吗?
或者你如何编译你的源文件?
我在使用 gradle 编译时收到了同样的错误消息,并且我在 gradle 构建文件中遗漏了一小行。
- 您需要一种机制来回送(即要求用户再次输入数据)以防输入无效。最常见的方法之一是使用
do-while
循环,它保证至少执行一次循环体。检查 this tutorial 以了解更多信息。
- 使用
Scanner::nextLine
而不是 Scanner::next
、Scanner::nextInt
等,因为它们不消耗通过按 Enter 键创建的换行符, 从输入。检查 this discussion 以了解更多信息。
- 最后但同样重要的是,永远不要为
System.in
关闭 Scanner
,因为它也会关闭 System.in
,并且如果不重新启动 JVM 就无法再次打开它。 注意:如果您正在使用Scanner
扫描File
,请务必关闭它以释放资源。
演示:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int option;
boolean valid;
Scanner input2 = new Scanner(System.in);
do {
valid = true;
System.out.println("Choose an option:\n" + "1-Addition\n" + "2-Subtraction\n" + "3-Exit");
try {
option = Integer.parseInt(input2.nextLine());
if (option < 1 || option > 3) {
throw new IllegalArgumentException();
}
// ...Place here the rest of code (which is based on the value of option)
} catch (IllegalArgumentException e) {
System.out.println("This is an invalid entry. Please try again.");
valid = false;
}
} while (!valid);
}
}
样本运行:
Choose an option:
1-Addition
2-Subtraction
3-Exit
x
This is an invalid entry. Please try again.
Choose an option:
1-Addition
2-Subtraction
3-Exit
10.5
This is an invalid entry. Please try again.
Choose an option:
1-Addition
2-Subtraction
3-Exit
1
我在 java 中遇到了一些问题,在 while 循环中将扫描器放在输出之后。 扫描仪扫描要走的方法,然后返回到循环的开始 重置变量。
我已经试过了,但没有找到任何可以理解的解决方案(我是 java 的新手,这对我来说很难), 或者自己解决。 这是完整的代码(我知道代码效率不高):
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int con = 1;
System.out.println("Hey!,welcome to my games!");
Scanner scanner = new Scanner(System.in);
String game;
while (con == 1) {
System.out.println(
"Here Some games i have(Enter the text for the one you want):\nto stop=0\n1)calculator=1\n2)!Soon! Random numbers");
game = scanner.nextLine();
calculator();
scanner.reset();
System.out.println(game);
// if(game.equals("0")) {
// con=0;
// }
// else if(game.equals("1")) {
// System.out.println("Welcome Back!");
// }
// else {
// System.out.println("There is a mistake in your text");
// }
}
scanner.close();
}
static void calculator() {
int num1, num2, con = 1, stop = 1;
String op, ad = "add", su = "sub", mul = "multi", di = "div";
Scanner af = new Scanner(System.in);
while (con == 1) {
stop = 1;
System.out.println("Write number 1");
num1 = af.nextInt();
System.out.println("Write number 2");
num2 = af.nextInt();
System.out.println(
"Write an operation (one of these):\nAddition=add\nSubtraction=sub\nMultiplication=multi\nDivision=div");
op = af.next();
op = op.toLowerCase();
int minus = num1 - num2;
int plus = num1 + num2;
if (op.equals(ad)) {
System.out.println("The Operation is:Addition\n" + num1 + "+" + num2 + "=" + plus);
} else if (op.equals(su)) {
System.out.println("The Operation is:Subtraction\n" + num1 + "-" + num2 + "=" + minus);
} else if (op.equals(mul)) {
System.out.println("The Operation is:Multiplication\n" + num1 + "*" + num2 + "=" + num1 * num2);
} else if (op.equals(di)) {
System.out.println("The Operation is:Division\n" + num1 + "/" + num2 + "=" + num1 / num2);
} else {
System.out.println("Um,Did you make a mistake in your text?\nDo you want the calculator again?");
String yn = af.next();
yn = yn.toLowerCase();
if (yn.equals("yes") || yn.equals("yep")) {
stop = 0;
}
}
if (stop == 1) {
con = 0;
}
}
af.close();
}
}
如您所见,我尝试自己解决它,甚至对一些代码发表了评论, 但是当它运行到该方法并返回时,它失败了,因为扫描器认为有 在我写东西之前要扫描的东西。这是例外-
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at Main.main(Main.java:12)
你记得导入 java.util.Scanner 吗? 或者你如何编译你的源文件? 我在使用 gradle 编译时收到了同样的错误消息,并且我在 gradle 构建文件中遗漏了一小行。
- 您需要一种机制来回送(即要求用户再次输入数据)以防输入无效。最常见的方法之一是使用
do-while
循环,它保证至少执行一次循环体。检查 this tutorial 以了解更多信息。 - 使用
Scanner::nextLine
而不是Scanner::next
、Scanner::nextInt
等,因为它们不消耗通过按 Enter 键创建的换行符, 从输入。检查 this discussion 以了解更多信息。 - 最后但同样重要的是,永远不要为
System.in
关闭Scanner
,因为它也会关闭System.in
,并且如果不重新启动 JVM 就无法再次打开它。 注意:如果您正在使用Scanner
扫描File
,请务必关闭它以释放资源。
演示:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int option;
boolean valid;
Scanner input2 = new Scanner(System.in);
do {
valid = true;
System.out.println("Choose an option:\n" + "1-Addition\n" + "2-Subtraction\n" + "3-Exit");
try {
option = Integer.parseInt(input2.nextLine());
if (option < 1 || option > 3) {
throw new IllegalArgumentException();
}
// ...Place here the rest of code (which is based on the value of option)
} catch (IllegalArgumentException e) {
System.out.println("This is an invalid entry. Please try again.");
valid = false;
}
} while (!valid);
}
}
样本运行:
Choose an option:
1-Addition
2-Subtraction
3-Exit
x
This is an invalid entry. Please try again.
Choose an option:
1-Addition
2-Subtraction
3-Exit
10.5
This is an invalid entry. Please try again.
Choose an option:
1-Addition
2-Subtraction
3-Exit
1