Search code examples
javaloopsincrementdecrement

How to Maintain Value of Previous Increment in Loop


Here is my code:

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

Here is my problem:

Let's say I run the program once and I choose to cast my vote for Mar Roxas. I enter the number 11. If I choose to stop the program, it tallies it up and says Mar Roxas gets one vote while the other guy gets 0. So far, so good. When I decide to choose to continue the loop, (which would re-run the program), it gets problematic.

When I decide to cast my vote on the other politician and decide to end the program, my initial vote on Mar Roxas becomes 0 and the Duterte gets 1.

How can I maintain the value of my previous votes when continuing the loop?


Solution

  • The following actions needed to be taken:

    • The vote counts for Roxas and Duterte need to be initialized outside of the do-while loop; this way, they don't reset back to zero within the loop.
    • Increment the vote counters during voting instead of when counting up the results. This way, they can actually hold values greater than 1.
    • Set yaya to a number other than 5 to break out of the loop (when voting has concluded).

    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