Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何让它运行直到用户输入0或负数?_Java_Loops - Fatal编程技术网

Java 如何让它运行直到用户输入0或负数?

Java 如何让它运行直到用户输入0或负数?,java,loops,Java,Loops,我很想知道如何让它运行直到用户输入0,我试着这样做,同时我将数字初始化为非零整数,并将所有内容包含在while循环中 public class Main { private static Scanner sc; public static void main(String args[]){ int number = 7; // can just use a random starting number which is not 0 while (

我很想知道如何让它运行直到用户输入0,我试着这样做,同时我将数字初始化为非零整数,并将所有内容包含在while循环中

public class Main {
    private static Scanner sc;

    public static void main(String args[]){
        int number = 7; // can just use a random starting number which is not 0
        while (number != 0) { // Add a while loop to make it keep asking for factorials until 0 is given to quit
            int i = 1, factorial = 1;
            System.out.println("Enter the number to which you need to find the factorial or enter 0 to quit:");
            sc = new Scanner(System.in);
            number = sc.nextInt();

            while (i <= number) {
                factorial = factorial * i;
                i++;
            }
            System.out.println("Factorial of the given number is:: " + factorial);
        }
        System.out.println("Thanks for using the factorial calculator. ");

    }
}

让它一直运行到用户输入0是什么意思?程序在完成阶乘计算后终止。你想让它一直接受新的因子直到用户退出吗?是的,Matthew,我当时想不出我的答案。
public class Main {
    private static Scanner sc;

    public static void main(String args[]){
        int number = 7; // can just use a random starting number which is not 0
        while (number != 0) { // Add a while loop to make it keep asking for factorials until 0 is given to quit
            int i = 1, factorial = 1;
            System.out.println("Enter the number to which you need to find the factorial or enter 0 to quit:");
            sc = new Scanner(System.in);
            number = sc.nextInt();

            while (i <= number) {
                factorial = factorial * i;
                i++;
            }
            System.out.println("Factorial of the given number is:: " + factorial);
        }
        System.out.println("Thanks for using the factorial calculator. ");

    }
}