简体   繁体   English

如何获得连续的用户输入?

[英]How do I get continuous user input?

I need a help trying to set my code to continuously receive user input for factorial numbers. 我需要帮助来尝试将代码设置为连续接收用户输入的阶乘数。 It will produce a question and intake the user input but only once. 它将产生一个问题并仅吸收一次用户输入。 I want it to continue asking the user for that input. 我希望它继续询问用户该输入。

I tried to do a while loop however nothing shows up. 我试图做一个while循环,但是什么也没出现。

import java.util.Scanner;
public class FactorialRecursion
{

    public static void main(String[] arg)
    {
            Scanner scan = new Scanner(System.in);
            long userInput;

            System.out.println("Please enter a number you would like find the factorial of.");
            userInput = scan.nextLong();
            long fc = FactorialRecursion.fact(userInput);
            System.out.println("Factorial = " + fc);
    }



    public static long fact(long x)
    {
            if (x <= 0)
                return 1;
            else
                return FactorialRecursion.fact(x - 1) * x;
    }

}

The output is correct but I want my program to continue asking for that input. 输出正确,但我希望我的程序继续要求该输入。

public class Test{
 public static void main(String []args) {
    int num;
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter numbers!");

    while((num = scanner.nextInt()) > 0) {
        System.out.println("Receiving...");
    }

    {
        System.out.println("Negative number Stopping the system...");
        System.exit(1);
    }
}

} }

Without knowing how you were looping before (my assumption is that you were including the scanner instantiation which might have caused an issue), here is an implementation that I believe will work for you. 在不知道您之前如何循环的情况下(我的假设是您包括可能导致问题的扫描程序实例化),我相信这是一个对您有用的实现。 This will continue to scan for a number, unless a negative number is entered. 除非输入负数,否则它将继续扫描一个数字。 Therefore you have an actual exit condition that doesn't make sense for factorial, and the user can repeatedly enter and find the factorial of positive numbers. 因此,您有一个实际的退出条件,对于阶乘没有意义,并且用户可以重复输入并找到正数的阶乘。

In order for this to work, I instantiated the userInput variable to be 0 so that the loop will run for the first time. 为了使它起作用,我将userInput变量实例化为0,以便循环将首次运行。 You can alteratively use a do...While loop in stead, but I prefer this method generally. 您可以替代地使用do ... While循环,但我通常更喜欢此方法。

public static void main(String[] arg)
  {

    Scanner scan = new Scanner(System.in);

    long userInput=0;
    while(userInput >=0)
    {
      System.out.println("Please enter a number you would like find the factorial of. Enter a negative number to exit.");

      userInput = scan.nextLong();

      long fc = FactorialRecursion.fact(userInput);

      System.out.println("Factorial = " + fc);
    }
  }

If you would like to see what the do-while loop would look like, just comment and I'll put a little more time into answering this. 如果您想看看do-while循环是什么样子,请发表评论,我将花更多的时间来回答这个问题。 Also any questions you have comment away! 另外,您有任何疑问都可以留言!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM