Java 如果回答“是”,则需要重复循环。For和Do while循环

Java 如果回答“是”,则需要重复循环。For和Do while循环,java,Java,对于我的程序,我需要计算用户输入的数字的阶乘,然后询问他们是否要输入另一个数字。如果是,则循环应重复自身并计算新数字的输入。对于程序,我需要使用For循环和do-while循环。我有计算阶乘部分的能力。然而,我很困惑,在输入“是”这个词后,如何让循环重复自身。非常感谢你的帮助。 是的,我知道目前程序中没有do-while循环,这就是我需要帮助的原因。我不知道如何处理整个情况。到目前为止,我的问题还没有解决。还说iv'e“没有完成我的研究”是不正确的,也无助于这种情况。谢谢你的帮助 import

对于我的程序,我需要计算用户输入的数字的阶乘,然后询问他们是否要输入另一个数字。如果是,则循环应重复自身并计算新数字的输入。对于程序,我需要使用For循环和do-while循环。我有计算阶乘部分的能力。然而,我很困惑,在输入“是”这个词后,如何让循环重复自身。非常感谢你的帮助。 是的,我知道目前程序中没有do-while循环,这就是我需要帮助的原因。我不知道如何处理整个情况。到目前为止,我的问题还没有解决。还说iv'e“没有完成我的研究”是不正确的,也无助于这种情况。谢谢你的帮助

import java.io.*;
import java.util.*;
public class Prog162a
{ //begin testshell 
public static void main (String[] args)
{ //begin main
    Scanner kbReader=new Scanner(System.in);

    //input 
    System.out.print("Enter a number: ");
    long number= kbReader.nextLong();
    long factorial= 1
    for ( int multiply=2;multiply<number;multiply++);
         { //begin value lopp
            factorial = factorial * multiply
            } // end value loop
       //output
       System.out.print("The calue of " +number+"! is " +multiply);
       System.out.println("\nWould you like to calculate another number?");

    }//end main
}//end testshell
import java.io.*;
导入java.util.*;
公共类Prog162a
{//begintestshell
公共静态void main(字符串[]args)
{//beginmain
扫描仪kbReader=新扫描仪(System.in);
//输入
System.out.print(“输入一个数字:”);
long number=kbReader.nextLong();
长阶乘=1

对于(int multiply=2;multiply,这里有一个基本结构,说明您希望使用do while循环来完成什么

String answer; //this will store the answer
do{ //starts loop
    //everything you're trying to do during the loop goes here
    System.out.println("Do you want to continue?"); //asks them if they want to continue
    answer = input.nextLine() //input will reference your scanner variable and puts answer into the variable we made
}while(answer.equals("yes"); //ends the loop when answer is equal to yes
希望这些评论能帮助你理解正在发生的事情


您很可能还希望确保他们回答是或否,并将他们的答案转换为小写或大写,这样当您检查它等于什么时,它就不区分大小写了。

这里我确实在循环的底部添加了一个if语句,以允许在用户输入“否”时退出

import java.io.*;
import java.util.*;
public class Prog162a
{ //begin testshell 
public static void main (String[] args)
{ //begin main
    Scanner kbReader=new Scanner(System.in);

    //input 
    System.out.print("Enter a number: ");
    long number= kbReader.nextLong();
    long factorial= 1
    for ( int multiply=2; multiply<number; multiply++);
         { //begin value loop
            factorial = factorial * multiply
            } // end value loop
       //output
       System.out.print("The value of " +number+"! is " +multiply);
       System.out.println("\nWould you like to calculate another number? Please enter yes or no");
       if(kbReader.nextLine().toLowerCase() == "no"){ //if user enters 'no' then it exits program  
        break; //this exits the loop (you could also use 'return;')
       }

    }//end main
}//end testshell
import java.io.*;
导入java.util.*;
公共类Prog162a
{//begintestshell
公共静态void main(字符串[]args)
{//beginmain
扫描仪kbReader=新扫描仪(System.in);
//输入
System.out.print(“输入一个数字:”);
long number=kbReader.nextLong();
长阶乘=1

对于(int multiply=2;multiply我尝试重用您的一些代码。基本上,我将您的代码移到了第二个函数(计算函数)中,并添加了一些缺少的分号。这个新函数在无休止的
while(true)中被调用
loop。在循环中每次调用后,都会要求用户键入yes。如果用户不键入yes,循环将自行中断并退出程序

private static Scanner kbReader = new Scanner(System.in);

public static void main(String[] args) {
    // endless loop (because I put true inside the brackets and not a variable check)
    while(true) {
        calculate(); // call your calculation code
        System.out.println("\nWould you like to calculate another number? (type 'yes'): ");
        // reads the command line input
        String input = kbReader.next();
        // the exclamation mark is a short form for saying that the result is false
        if (!input.equals("yes")) break; // leaving the loop
    }

}

private static void calculate() {
    System.out.print("Enter a number: ");
    long number = kbReader.nextLong();
    long factorial = 1;
    for (int multiply = 2; multiply < number + 1; multiply++){
        factorial = factorial * multiply;
    }
    System.out.print("The value of " + number + "! is " + factorial);
}
专用静态扫描仪kbReader=新扫描仪(System.in);
公共静态void main(字符串[]args){
//无止境循环(因为我将true放在括号内,而不是变量检查)
while(true){
calculate();//调用您的计算代码
System.out.println(“\n是否要计算另一个数字?(键入“是”):”;
//读取命令行输入
字符串输入=kbReader.next();
//感叹号是表示结果为假的简短形式
如果(!input.equals(“yes”))中断;//退出循环
}
}
私有静态void计算(){
System.out.print(“输入一个数字:”);
long number=kbReader.nextLong();
长阶乘=1;
对于(整数乘法=2;乘法<数字+1;乘法++){
阶乘=阶乘*乘;
}
系统输出打印(“值“+number+”!为“+factorial”);
}

编辑
注意for循环内的
multiplay
。不用说
number+1
6的结果是120(不是720).

您尝试过任何循环吗?如果是,问题出在哪里?我没有看到任何尝试进行do/while循环的情况。您对此做过任何研究吗?您想要的是与其他任何循环一样的循环。您希望在循环中重复什么代码?什么条件会退出循环?如果是这种情况,那么您肯定没有做任何研究。再次n、 粗略搜索
java do while input
将为您提供所需内容。您正在描述一个do/while循环。您显然没有阅读我前面包含的链接。它专门介绍了以下内容:要执行do循环,计算机首先执行循环体,即循环中的一个或多个语句,然后计算布尔表达式。“do-while在哪里?你不需要do-while,但你肯定可以使用常规的while循环。插入if语句对于他当前的解决方案来说是最简单的。来自OPs问题:“对于程序,我需要使用for循环和do-while循环”啊-我没有看到。让我看看我是否可以修复itOk,所以我这样做了,现在循环不会重复。它只是问问题,不让用户输入是或否,只完成循环。do{for(long multily2=2;multily2