Java 如何让它运行直到用户输入0或负数?
我很想知道如何让它运行直到用户输入0,我试着这样做,同时我将数字初始化为非零整数,并将所有内容包含在while循环中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 (
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. ");
}
}