嵌套循环有两个while循环

问题描述 投票:0回答:1

所以我必须提出一个输出报告的代码,我必须在另一个while循环中包含这个while循环,这个循环会询问用户是否要继续输入信息。只要用户响应“Y”,继续询问所需输入并打印报告。一旦用户回答“N”,则停止进一步处理。

我刚刚完成了生成报告的循环,但是我不知道如何用另一个具有上述要求的循环来封闭这个循环。

我目前的报告代码是这样的:

while (bookVal > salvageVal){
    //read in stuff

    yearlyDepr = bookVal * (ddRate / 100);
    accDepr = accDepr + yearlyDepr;
    bookVal = bookVal - yearlyDepr;
    year++;

    if (bookVal < salvageVal){
        yearlyDepr = (bookVal + yearlyDepr) - salvageVal;
        accDepr = purchPrice - salvageVal;
        bookVal = salvageVal;

    }
    System.out.printf("%d %,18.0f %,18.0f %,18.0f%n" , year, yearlyDepr, accDepr, bookVal);
}

我尝试在这个循环的结尾处设置一个“读入Y或N”并在其上面设置另一个while循环,例如:while(answer =='Y'),但这只是把所有东西都搞定了......一些帮助会很好,谢谢!

java loops while-loop nested-loops
1个回答
0
投票
Scanner s=new Scanner(System.in);
String toContinue="y";
while( (your code) && "y".equalsIgnoreCase(toContinue))
{
      //your code
      System.out.println("Do you want to continue : (Y-to continue,any otherto stop )");
      toContinue=s.next();
}
© www.soinside.com 2019 - 2024. All rights reserved.