如何在循环中保持先前增量的值

How to Maintain Value of Previous Increment in Loop

这是我的代码:

int yaya = 5;
int x = 10;
do {
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     System.out.println("Vote Ballot");
     System.out.println("Below are the 2 Canditates you can choose to vote from");
     System.out.println("Mar Roxas --- Code: 11");
     System.out.println("Duterte ---- Code: 12"); 
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     System.out.println("Who do you vote? Enter numbers only!"); 
     int choice = input.nextInt();
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     if (choice == 11) 
     {

         System.out.println("You have voted Mar Roxas and not Duterte");

     }
     else if ( choice == 12 ) 
     {

         System.out.println("You have voted Duterte and not Mar Roxas");

     }
     else 
     {

         System.out.println("You have entered an invalid number");

     }
     String confirm = "confirm";
     String deny = "deny";
     int conf = 1;
     int den = 2;
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     System.out.println("Do you want to let another voter vote? Or would you like to end the program at hand?");

     int ans = input.nextInt();
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     if ( ans==1 )
     {

         System.out.println("The program would now repeat");

     }
     else if ( ans==2 ) 
     {

          if ( choice ==11 )
          {
              int RoxasC = 0;
              int DuterteC = 0;
              RoxasC+=1;

              System.out.println("Mar roxas recieved " +RoxasC+ " number of vote/s and Duterte Recieved " +DuterteC+ 
              " number of votes");

          }
          else if ( choice ==12)
          {
              int RoxasC = 0;
              int DuterteC = 0;

              DuterteC+=1;
              System.out.println("Duterte recieved " +DuterteC+ " number of vote/s Roxas received " +RoxasC+ 
              " number of votes");

          }
          System.out.println("Program will end as per request");
          break;

     }
     else
     {
         System.out.println("You entered an invalid keyword program would still repeat");
     }

     System.out.println("\n");

} while( yaya==5 ); //Program Runs Infinitely

这是我的问题:

假设我 运行 参加过一次节目,我选择投票给 Mar Roxas。我输入数字 11。如果我选​​择停止该程序,它会计算并显示 Mar Roxas 获得一票,而另一个人获得 0 票。到目前为止,一切顺利。当我决定选择继续循环时,(这将重新 运行 程序),它会出现问题。

当我决定投票给另一位政客并决定结束该计划时,我对 Mar Roxas 的初始投票变为 0,而 Duterte 获得 1。

如何在继续循环时保持之前投票的价值?

这将解决您的问题problem.Actually您在每次循环迭代中都重新初始化了您的计数器,因此您的 RoxasC 和 DuterteC 计数器在每次循环迭代中都被重新初始化为零

int yaya = 5;
   int x = 10;
   int RoxasC = 0;
   int DuterteC = 0;
    do{
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     System.out.println("Vote Ballot");
     System.out.println("Below are the 2 Canditates you can choose to vote from");
     System.out.println("Mar Roxas --- Code: 11");
     System.out.println("Duterte ---- Code: 12"); 
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     System.out.println("Who do you vote? Enter numbers only!"); 
     int choice = input.nextInt();
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     if (choice == 11) 
     { 
         RoxasC+=1;
         System.out.println("You have voted Mar Roxas and not Duterte");


     } else if ( choice == 12 ) 
     {

          DuterteC+=1;
         System.out.println("You have voted Duterte and not Mar Roxas");


     }
     else 
     {

         System.out.println("You have entered an invalid number");

     }
     String confirm = "confirm";
     String deny = "deny";
     int conf=1;
     int den=2;
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     System.out.println("Do you want to let another voter vote? Or would you like to end the program at hand?");

     int ans = input.nextInt();
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     if ( ans==1 )
     {

         System.out.println("The program would now repeat");



     }
     else if (ans==2 ) 
     {


          if ( choice ==11 )
          {  



              System.out.println("Mar roxas recieved " +RoxasC+ " number of vote/s and Duterte Recieved " +DuterteC+ 
              " number of votes");

          } else if ( choice ==12)
          {



              System.out.println("Duterte recieved " +DuterteC+ " number of vote/s Roxas received " +RoxasC+ 
              " number of votes");


          }
          System.out.println("Program will end as per request");
          break;


     } else
     {
         System.out.println("You entered an invalid keyword program would still repeat");
        }

     System.out.println("\n");

   }while( yaya==5 ); //Program Runs Infinitely
Scanner input = new Scanner(System.in);

int RoxasC = 0;
int DuterteC = 0;

int yaya = 5;
   int x = 10;
    do{
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     System.out.println("Vote Ballot");
     System.out.println("Below are the 2 Canditates you can choose to vote from");
     System.out.println("Mar Roxas --- Code: 11");
     System.out.println("Duterte ---- Code: 12"); 
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     System.out.println("Who do you vote? Enter numbers only!"); 
     int choice = input.nextInt();
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     if (choice == 11) 
     { 

         System.out.println("You have voted Mar Roxas and not Duterte");


     } else if ( choice == 12 ) 
     {


         System.out.println("You have voted Duterte and not Mar Roxas");


     }
     else 
     {

         System.out.println("You have entered an invalid number");

     }
     String confirm = "confirm";
     String deny = "deny";
     int conf=1;
     int den=2;
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     System.out.println("Do you want to let another voter vote? Or would you like to end the program at hand?");

     int ans = input.nextInt();
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     if ( ans==1 )
     {

         System.out.println("The program would now repeat");


          if ( choice ==11 )
          {  

              RoxasC+=1;

              System.out.println("Mar roxas recieved " +RoxasC+ " number of vote/s and Duterte Recieved " +DuterteC+ 
              " number of votes");

          } else if ( choice ==12)
          {


              DuterteC+=1;
              System.out.println("Duterte recieved " +DuterteC+ " number of vote/s Roxas received " +RoxasC+ 
              " number of votes");


          }


     }
     else if (ans==2 ) 
     {


          if ( choice ==11 )
          {  



              System.out.println("Mar roxas recieved " +RoxasC+ " number of vote/s and Duterte Recieved " +DuterteC+ 
              " number of votes");

          } else if ( choice ==12)
          {


              System.out.println("Duterte recieved " +DuterteC+ " number of vote/s Roxas received " +RoxasC+ 
              " number of votes");


          }
          System.out.println("Program will end as per request");
          break;


     } else
     {
         System.out.println("You entered an invalid keyword program would still repeat");
        }

     System.out.println("\n");

   }while( yaya==5 ); //Program Runs Infinitely 

需要采取以下行动:

  • Roxas 和 Duterte 的投票计数需要在 do-while 循环之外进行初始化;这样,它们就不会在循环中重置为零。
  • 在投票期间增加投票计数器,而不是在统计结果时。这样,它们实际上可以保存大于 1 的值。
  • yaya 设置为 5 以外的数字以跳出循环(当投票结束时)。

Scanner input = new Scanner(System.in);
int yaya = 5;
int x = 10;
int RoxasC = 0;
int DuterteC = 0;

do {
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     System.out.println("Vote Ballot");
     System.out.println("Below are the 2 Canditates you can choose to vote from");
     System.out.println("Mar Roxas --- Code: 11");
     System.out.println("Duterte ---- Code: 12"); 
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     System.out.println("Who do you vote? Enter numbers only!"); 
     int choice = input.nextInt();
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     if (choice == 11) 
     {

         System.out.println("You have voted Mar Roxas and not Duterte");
         RoxasC++;

     }
     else if ( choice == 12 ) 
     {

         System.out.println("You have voted Duterte and not Mar Roxas");
         DuterteC++;

     }
     else 
     {

         System.out.println("You have entered an invalid number");

     }
     String confirm = "confirm";
     String deny = "deny";
     int conf = 1;
     int den = 2;
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     System.out.println("Do you want to let another voter vote? Or would you like to end the program at hand?");

     int ans = input.nextInt();
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     if ( ans==1 )
     {

         System.out.println("The program would now repeat");

     }
     else if ( ans==2 ) 
     {
          System.out.println("Mar Roxas received " +RoxasC+ " number of votes, and Duterte received " +DuterteC+ 
              " number of votes");

          System.out.println("Program will end as per request");
          yaya = 0;
          break;

     }
     else
     {
         System.out.println("You entered an invalid keyword program would still repeat");
     }

     System.out.println("\n");

} while( yaya==5 ); //Program Runs Infinitely