2015-12-02 19 views
-2

這裏是我的代碼:如何維持前期增量的價值循環

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 

這裏是我的問題:

比方說,我運行程序一次,我選擇投我的馬·羅哈斯投票。我輸入數字11.如果我選擇停止該計劃,它會對它進行合併,並說Mar Roxas得到一票,而另一個得到0.到目前爲止,這麼好。當我決定選擇繼續循環(將重新運行程序)時,它會出現問題。

當我決定投上其他政客我的投票,決定結束程序,我對馬·羅哈斯初始票變爲0,Duterte得到1

我怎樣才能保持我以前的票值當繼續循環?

+0

您初始化整數0的循環中。只要你離開那個if區塊,那些變量就會超出範圍。如果你想保留它們,你必須在循環之外聲明它們。 – Arc676

+0

@JosephAndrews您是指我的答案,還是您的意思是迴應Arc676? –

回答

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 
+0

謝謝! @ChrisForrence。我意識到我的錯誤, –

0

這將解決您的problem.Actually在每次循環迭代你重新initializaingg你再次櫃檯所以你RoxasC和DuterteC計數器正在重新initializad到零上每次循環

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 
+0

Haseeb,當你在正確的軌道上初始化do-while循環之外的投票計數器時,你錯過了一些東西;在你的代碼中,當投票計數器增加了嗎? –

+0

RoxasC + = 1;和DuterteC + = 1;在條件成立的情況下,if條件將遞增計數器。 @chrisForrence –

+0

好吧,其中if-condition?投票的人還是決定投票是否繼續的人? –

1
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 
+0

這是第一個可能正確的解決方案;如果投票應繼續進行,您在循環中遞增計數器,因此投票計數器將在程序運行時累計。 –