嵌套循环有两个while循环

时间:2018-03-02 04:12:22

标签: java loops while-loop nested-loops

所以我必须提出一个输出报告的代码,我必须将这个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);
}

我试过把一个&#34;读入Y或N&#34;在这个循环结束时,在它上面设置另一个while循环,例如:while(answer ==&#39; Y&#39;),但这只是把所有东西都搞定了......有些帮助会很好,谢谢!

1 个答案:

答案 0 :(得分: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();
}
相关问题