【问题标题】:Do Loops to create read user input to repeat or exit program Java执行循环以创建读取用户输入以重复或退出程序 Java
【发布时间】:2014-04-09 02:30:11
【问题描述】:

这是我的第一篇文章。

使用 Scanner 类,我试图让用户输入选择重复程序或退出。问题是我的 Do 循环语句重复了程序并且即使 Do 循环为假并且应该退出程序也不会退出。

// loop repeat or quit
do { 
 //initialize variable
  int integer;
  int x = 1;
  int factorial = 1;

System.out.print("Please enter an integer \n");
integer = getInt.nextInt();
//loop for factorial 
//multiple each increment until it reaches the integer
while (x <= integer) {

  factorial *= x;
  x++;

 }; // factorial=x*x
  System.out.println("the factorial of the integer " + integer + " is " + factorial);

 System.out.print("do you want to quit? y or n \n");
 quit = getString.next();


 } while(quit != yes);


 System.exit(0);
 }

【问题讨论】:

  • 忽略此代码甚至无法编译,可能与 How do I compare strings in Java? 重复
  • @user3502443 OP 请通过单击所选解决方案左侧的复选标记来选择正确答案。

标签: java loops do-while


【解决方案1】:

你的代码有一些错误,所以我重写了一点,并在你使用不正确的地方使用了正确的功能。

    public static void main(String args[])
    {
    // Scanner is used to take in inputs from user
    Scanner scan = new Scanner (System.in);
    String quit = "";

    // loop repeat or quit
    do { 
        //initialize variable
        int integer = 0;
        int x = 1;
        int factorial = 1;

        // User needs to enter integer, or it'll throw exception.
        System.out.println("Please enter an integer");
        integer = scan.nextInt();

        //loop for factorial 
        //multiple each increment until it reaches the integer
        // factorial = x!
        while (x <= integer) {
            factorial *= x;
            x++;
        }; 

        System.out.println("the factorial of the integer " + integer + " is " + factorial);
        System.out.println("do you want to quit? y or n");
        quit = scan.next();

         // if quit is NOT equal to y, we do it again
     } while(!quit.equals("y"));

     System.exit(0);
}

我希望 cmets 有所帮助:)

【讨论】:

  • 谢谢这是完美的
【解决方案2】:

我已经编辑了您的代码,它现在可以运行了。

供将来参考:包括更全面的 sn-ps,以便代码的查看者更容易发现错误。

问题:无法保证用户只输入 y 而没有任何空格。解决这个问题的简单方法是使用字符串方法contains()。我已经修改了您的循环,以便如果用户输入 y 程序将退出并且它现在可以工作。让我知道这是否可行并且编码愉快!

public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        String quit ="";

        do { //initialize variable
            int integer; int x = 1; int factorial = 1;

             System.out.print("Please enter an integer \n");
             integer = in.nextInt();
             //loop for factorial 
             //multiple each increment until it reaches the integer
             while (x <= integer) {

               factorial *= x;
               x++;

              }; // factorial=x*x
               System.out.println("the factorial of the integer " + integer + " is " + factorial);

              System.out.print("do you want to quit? y or n \n");
              quit = in.next();


              } while(!quit.contains("y"));


              System.exit(0);


    }

【讨论】:

  • 为什么我不能使用这个语句“(quit != "yes")”。它具有正确的语法。在我看来逻辑上是正确的。
  • 会的。问题是您可能没有考虑到空白字符。
【解决方案3】:

不应该吗

while(quit != "y");

我也不明白为什么你使用System.out.print();,然后在有一个非常好的System.out.pritnln();时使用\n

另外,因为我们正在处理字符串,所以 .nextLine();对于扫描仪来说已经足够了。 (您还必须声明 String 退出。)

【讨论】:

猜你喜欢
  • 2018-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-04
  • 2019-08-29
  • 2014-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多