在线程 "main" java.util.InputMismatchException 中获取异常
Getting exception in thread "main" java.util.InputMismatchException
我正在尝试一个一个地输入,然后尝试对它们执行操作。按照我的代码。
import java.util.Scanner;
public class PlayerRoster {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
final int LENGTH = 5;
final char quit = 'q';
char uin = 'z';
int jersey = 0;
int chng = 0;
int[] number = new int[LENGTH];
int[] rating = new int[LENGTH];
for(int i = 0; i < LENGTH; i++){
System.out.println("Enter player "+(i+1)+"'s jersey number:");
number[i] = scnr.nextInt();
System.out.println("Enter player "+(i+1)+"'s rating:\n");
rating[i] = scnr.nextInt();
}
for(int i = 0; i < LENGTH; i++){
if (i == 0)
System.out.println("ROSTER");
System.out.println("Player " + (i + 1) + " -- Jersey number: " + (number[i]) + ", Rating: " + (rating[i]));
if (i == 4)
System.out.println("");
}
while (uin != quit){
System.out.println("MENU");
System.out.println("u - Update player rating");
System.out.println("a - Output players above a rating");
System.out.println("r - Replace player");
System.out.println("o - Output roster");
System.out.println("q - Quit\n");
System.out.println("Choose an option:");
uin = scnr.next().charAt(0);
if (uin == 'o')
for(int i = 0; i < LENGTH; i++){
if (i == 0)
System.out.println("ROSTER");
System.out.println("Player " + (i + 1) + " -- Jersey number: " + (number[i]) + ", Rating: " + (rating[i]));
if (i == 4)
System.out.println("");
}
if (uin == 'u')
System.out.println("Enter a jersey number: ");
chng = scnr.nextInt();
}
}
}
我目前不专注于逻辑,因为我无法接受输入,因为我正在 Exception in thread "main" java.util.InputMismatchException
当我只是想获得更多用户输入时。
我的输入是:
20 5 30 2 50 4 60 8 93 9 u 20 q
我已经尝试了从 char
到正常 Strings
的所有方法。感觉就像扫描仪不工作一样。
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:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at PlayerRoster.main(PlayerRoster.java:55)
问题是代码块
if (uin == 'u')
System.out.println("Enter a jersey number: ");
chng = scnr.nextInt();
因为你只需要球衣号码,以防用户输入 'u' your chng = scnr.nextInt();应该在 if 块内。
在您的情况下,即使用户将输入作为 'q',它仍然期望用户提供更多输入,并且您可能只是给出空白 space 在这种情况下给您 InputMisMatchException.
要修复它,请将代码更新为
if (uin == 'u')
{
System.out.println("Enter a jersey number: ");
chng = scnr.nextInt();
}
所以现在,如果您将输入作为 'q',您的循环将停止执行,这是预期的输出。
我正在尝试一个一个地输入,然后尝试对它们执行操作。按照我的代码。
import java.util.Scanner;
public class PlayerRoster {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
final int LENGTH = 5;
final char quit = 'q';
char uin = 'z';
int jersey = 0;
int chng = 0;
int[] number = new int[LENGTH];
int[] rating = new int[LENGTH];
for(int i = 0; i < LENGTH; i++){
System.out.println("Enter player "+(i+1)+"'s jersey number:");
number[i] = scnr.nextInt();
System.out.println("Enter player "+(i+1)+"'s rating:\n");
rating[i] = scnr.nextInt();
}
for(int i = 0; i < LENGTH; i++){
if (i == 0)
System.out.println("ROSTER");
System.out.println("Player " + (i + 1) + " -- Jersey number: " + (number[i]) + ", Rating: " + (rating[i]));
if (i == 4)
System.out.println("");
}
while (uin != quit){
System.out.println("MENU");
System.out.println("u - Update player rating");
System.out.println("a - Output players above a rating");
System.out.println("r - Replace player");
System.out.println("o - Output roster");
System.out.println("q - Quit\n");
System.out.println("Choose an option:");
uin = scnr.next().charAt(0);
if (uin == 'o')
for(int i = 0; i < LENGTH; i++){
if (i == 0)
System.out.println("ROSTER");
System.out.println("Player " + (i + 1) + " -- Jersey number: " + (number[i]) + ", Rating: " + (rating[i]));
if (i == 4)
System.out.println("");
}
if (uin == 'u')
System.out.println("Enter a jersey number: ");
chng = scnr.nextInt();
}
}
}
我目前不专注于逻辑,因为我无法接受输入,因为我正在 Exception in thread "main" java.util.InputMismatchException
当我只是想获得更多用户输入时。
我的输入是:
20 5 30 2 50 4 60 8 93 9 u 20 q
我已经尝试了从 char
到正常 Strings
的所有方法。感觉就像扫描仪不工作一样。
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:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at PlayerRoster.main(PlayerRoster.java:55)
问题是代码块
if (uin == 'u')
System.out.println("Enter a jersey number: ");
chng = scnr.nextInt();
因为你只需要球衣号码,以防用户输入 'u' your chng = scnr.nextInt();应该在 if 块内。 在您的情况下,即使用户将输入作为 'q',它仍然期望用户提供更多输入,并且您可能只是给出空白 space 在这种情况下给您 InputMisMatchException.
要修复它,请将代码更新为
if (uin == 'u')
{
System.out.println("Enter a jersey number: ");
chng = scnr.nextInt();
}
所以现在,如果您将输入作为 'q',您的循环将停止执行,这是预期的输出。