如何维持循环中先前增量的价值

时间:2015-12-02 12:23:23

标签: java loops increment decrement

这是我的代码:

<?php


$first = "/calvin/master/?p=pages&id=3";
$second = "http://localhost/calvin/";

//First we explode both string to have array which are easier to compare
$firstArr = explode('/', $first);
$secondArr = explode('/', $second);

//Then we merged those array and remove duplicata
$fullArray = array_merge($secondArr,$firstArr);
$fullArray = array_unique($fullArray);

//And we put all back into a string
$fullStr = implode('/', $fullArray);
?>

这是我的问题:

让我说我运行一次程序,然后我选择投票给Mar Roxas。我输入了11号。如果我选​​择停止该程序,它就会计算出来并说Mar Roxas获得一票而另一个人获得0票。到目前为止,这么好。当我决定继续循环时(会重新运行程序),就会出现问题。

当我决定对另一位政治家投票并决定结束该计划时,我对Mar Roxas的初步投票变为0,而Duterte的投票结果为1。

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

3 个答案:

答案 0 :(得分:1)

create table t3 (Code int,Val varchar(50),Val1 varchar(20))
create table t2 (Code int,Val varchar(50),Val1 varchar(20))


insert into t2 Values (1,'20', '25')
insert into t2 Values (2,'50',50)

insert into t3 Values (1,'20',25)
insert into t3 Values (23,'5',50)

Select * from ( Select BINARY_CHECKSUM(val,val1) chk, * from  t3 ) a
full outer join (Select BINARY_CHECKSUM(val,val1) chk, *  from  t2 ) b on a.chk = b.chk

答案 1 :(得分:0)

这将解决您的问题。实际上,您在每次循环迭代时重新初始化计数器,因此您的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

答案 2 :(得分:0)

需要采取以下行动:

  • 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