0

有谁知道如何停止或继续循环。我想问用户您要继续(是)还是(否)?...当我在此代码中执行此操作时,循环退出。我究竟做错了什么?我在这里先向您的帮助表示感谢。

import java.util.Scanner;
public class salarywithdoloop {
    public static void main(String args[]) {
        Scanner kb = new Scanner(System.in);
        int salary = 0;
        double federaltax = 0;
        double netsalary = 0;
        double totaltax = 0;
        double statetax = 0;
        int over_100000 = 0;
        int between50_100000 = 0;
        int between25_50000 = 0;
        int below25000 = 0;
        String stop = "";
        int count = 0;
        do {
            System.out.println("Please Input your salary");
            salary = kb.nextInt();
            if (salary >= 100000) {
                statetax = salary * .05;
                federaltax = salary * .20;
                netsalary = statetax + federaltax;

            } else {
                statetax = salary * .05;
                federaltax = salary * .15;
                netsalary = statetax + federaltax;

            }
            if (salary >= 100000) {
                over_100000 = over_100000 + 1;
            } else if (salary >= 50000 && salary <= 100000) {
                between50_100000 = between50_100000 + 1;
            } else if (salary >= 25000 && salary <= 50000) {
                between25_50000 = between25_50000 + 1;
            } else {
                below25000 = below25000 + 1;
            }

            System.out.println("Federal tax :" + federaltax);
            System.out.println("netsalary :" + netsalary);
            System.out.println("statetax :" + statetax);
            System.out.println("salary :" + salary);
            System.out.println("Over 100000: " + over_100000);
            System.out.println("Between 50000 and 100000: " + between50_100000);
            System.out.println("Between 25000 and 50000: " + between25_50000);
            System.out.println("Below 25000:" + below25000);

            System.out.println("Do you want to continue?");
            stop = kb.nextLine();
            kb.nextLine();

            if (stop.equals("yes")) {
                continue;
            } else if (stop.equals("no")) {
                break;
            }
        } while (0 <= count);
    }     
}
4

7 回答 7

2

只需将您的while条件更改do-while为此。

while(stop.equals("yes"));

然后你可以删除循环if-else中的。do-while

// The below if-else is not required at all, I commented it, but you can delete it.
//if (stop.equals("yes")) {
    //continue;
//} else if (stop.equals("no")) {
    //break;
//}

你需要颠倒这两个readLine()陈述。

kb.nextLine(); // line 1, this will consume the enter
stop = kb.nextLine(); // line 2, // this will actually get the next user input
于 2013-11-14T04:49:37.673 回答
0
break will quit the loop
continue will basically go to the top of the loop

所以你想要的逻辑是

while (true) {

   if (stop.equals("yes")) continue;
   break;

}
于 2013-11-14T04:49:42.410 回答
0

continue进入 do..while 循环的下一次迭代;break退出循环。在代码中切换continuebreak

于 2013-11-14T04:51:07.313 回答
0

改变

System.out.println("Do you want to continue?"); 
           stop = kb.nextLine();
           kb.nextLine();

System.out.println("Do you want to continue?"); 
           stop = kb.nextLine();
于 2013-11-14T04:51:49.353 回答
0

您的问题将通过以下方式解决:

System.out.println("Do you want to continue?");
stop = kb.nextLine();
stop = kb.nextLine();

if (stop.equals("yes"))
{
    continue;
} else if (stop.equals("no"))
{
    break;
}
于 2013-11-14T04:54:03.767 回答
0

尝试这个:

           System.out.println("Do you want to continue?"); 
//            stop = kb.nextLine();
//            kb.nextLine();
           stop = kb.next();
于 2013-11-14T05:00:56.083 回答
-1

尝试这个

 public static void main(String args[])
  {

       Scanner kb = new Scanner(System.in);
       int salary = 0;
       double federaltax =0;
       double netsalary=0;
       double totaltax = 0;
       double statetax=0;
       int over_100000=0;
       int between50_100000=0;
       int between25_50000=0;
       int below25000=0;
       String stop="";
       int count=0;
       do
       {
           System.out.println("Please Input your salary");
           salary = kb.nextInt();
                            if(salary >= 100000) 
                            {
                                 statetax= salary * .05;
                                 federaltax = salary * .20; 
                                 netsalary = statetax + federaltax;

                            }
                            else
                            {
                                 statetax= salary * .05;
                                 federaltax = salary * .15; 
                                 netsalary = statetax + federaltax;                  

                            }
                            if(salary >= 100000) 
                            {
                             over_100000 =  over_100000 + 1;
                            }
                            else if(salary >= 50000 && salary <=100000) 
                            {
                             between50_100000 =  between50_100000 + 1;
                            }
                            else if(salary >= 25000 && salary <=50000) 
                            {
                             between25_50000 =  between25_50000 + 1;
                            }
                            else
                            {
                              below25000 = below25000 + 1;
                            }


           System.out.println("Federal tax :" + federaltax);
           System.out.println("netsalary :" + netsalary);
           System.out.println("statetax :" + statetax);
           System.out.println("salary :" + salary);
           System.out.println("Over 100000: " + over_100000);
           System.out.println("Between 50000 and 100000: " + between50_100000);
           System.out.println("Between 25000 and 50000: " +  between25_50000);
           System.out.println("Below 25000:" +  below25000);

           System.out.println("Do you want to continue?"); 
           stop = kb.nextLine();

      }while(stop.equals("yes"));

   }  
于 2013-11-14T04:59:08.207 回答