【问题标题】:Java Even Odd Integer adding Yes or No question to play againJava Even Odd Integer 添加是或否问题再次播放
【发布时间】:2020-03-19 10:57:18
【问题描述】:

我对 Java 很陌生,正在努力完成一项任务。我已经阅读了几篇文章,但似乎无法弄清楚如何将“Enter·y·to·play·again,·n·to·quit(y/n)”添加到我的代码中。这是我到目前为止所拥有的。任何方向将不胜感激。我不知道如何让它返回输入一个数字。谢了

//Program to display Even or Odd Integer
import java.util.Scanner;

public class EvenOdd {
    public static void main(String[] args) { //method to determine is a number is even or odd
        Scanner input = new Scanner(System.in);
        int number; //number o be entered
        System.out.print("Enter one number: "); //prompt user to enter a number
        number = input.nextInt();
        switch (number % 2) {
            case 0:
                System.out.printf("%d is even\n", number);
            case 1:
                System.out.printf("%d is odd\n", number);
        }
        System.out.print("Enter Y to play again, N to quit: "); //prompt user to enter a number
        number = input.nextInt();
    }

    public boolean isEven(int number) {
        return number %2 == 0;
    } //end method isEven
} //end class EvenOdd

结果:

Enter one number: 1
1 is odd
Enter Y to play again, N to quit: n
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at EvenOdd.main(EvenOdd.java:21)

【问题讨论】:

  • 您需要一个循环,对于 y/n 问题,您必须读取一个字符串
  • 您打印“输入 Y 再次播放,N 退出”然后您调用 nextInt()。为什么你会相信 int 可以接受 YN 字符?
  • YN 不是数字。 InputMismatchException 表示您希望用户输入一个数字,但她输入了一个字母。您需要使用方法next() 来接受用户的YN
  • @Abra 如果你使用nextLine(),你会遇到这个问题:Scanner is skipping nextLine() after using next() or nextFoo()?
  • 谢谢。对编程非常陌生,信息量是压倒性的,最小的事情可能会非常令人沮丧。我可以在关于不匹配的回复中看到正在运行的主题(显示在错误中)。我知道这是相关的,但没有经验并且无法理解修复。我想我明白了——见下文。再次感谢您的快速回复。未来可能会有更多问题。

标签: java if-statement methods while-loop do-while


【解决方案1】:

您得到的异常告诉您输入与您尝试分配的正确类型不匹配。基本上:您正在尝试将类型String(例如“Y”/“N”)分配给int。这将引发错误。在此处阅读有关不同类型的更多信息:https://en.wikibooks.org/wiki/Java_Programming/Primitive_Types

以下是有关如何解决此问题的建议。您还会注意到,我在您的程序中添加了一个 while 循环,以便能够再次播放。 您要更改的代码是input.nextInt()input.next()。您还想更改分配给哪个变量,因为您不能将String 分配给int。 还将break; 添加到您的switch 语句中,因此不会遍历每个案例。

public class Main {

    public static void main(String[] args) { //method to determine is a number is even or odd
        String cont = "Y";
        Scanner input = new Scanner(System.in);
        while (cont.equals("Y")) {
            int number; //number o be entered
            System.out.print("Enter one number: "); //prompt user to enter a number
            number = input.nextInt();
            switch (number % 2) {
                case 0:
                    System.out.printf("%d is even\n", number);
                    break; //NOTICE here
                case 1:
                    System.out.printf("%d is odd\n", number);
                    break; //NOTICE here
            }
            System.out.print("Enter Y to play again, N to quit: "); //prompt user to enter a number
            cont = input.next(); // <---- This is what you want 
        }
        System.out.println("Did not want to play again, exiting...");
    }
}

【讨论】:

  • 谢谢。从最初的回复中,我能够理解错误,但不能理解修复。将数字转换为字母没有意义。您的意见非常有用:
  • 输入一个数字:6 6 为偶数 输入 y 重新播放,n 退出(y/n):y 输入一个数字:5 5 为奇数 输入 y 重新播放,n 退出( y/n): n 用户不想继续...
猜你喜欢
  • 1970-01-01
  • 2012-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-22
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
相关资源
最近更新 更多