在线程“main”java.util.inputmismatchexception中获取异常

j8ag8udp  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(307)

我试着一个接一个地接受输入,然后试着对它们执行操作。跟着我的密码。

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)
fjaof16o

fjaof16o1#

问题是代码块

if (uin == 'u')
        System.out.println("Enter a jersey number: ");
           chng = scnr.nextInt();

因为您只需要在用户输入'u'时输入泽西号码,所以chng=scnr.nextint();应该在if块内。在您的例子中,即使用户以“q”的形式输入,它仍然期望用户提供更多的输入,而且在这种情况下,您可能只是在给您输入不匹配异常的空格。
要修复它,请将代码更新为

if (uin == 'u')
{
     System.out.println("Enter a jersey number: ");
     chng = scnr.nextInt();
}

所以现在如果你把输入设为q,你的循环就会停止执行,这就是预期的输出。

相关问题