2017-04-22 73 views
-1

最近開始計算機編程,我被困在家庭作業上。我創建了一個循環,但不是回到頂端,而是從我打算的位置開始提前一步。 (這一切都在我的編碼提出的意見列出。我一直在試圖弄清楚這一點,過去6小時,這將不勝感激,如果有人能幫助我。做/ while循環不回到頂部?

import java.util.Scanner; 
import java.text.DecimalFormat; 

public class Election 
{ 
    public static void main (String[] args) 
     { 

      DecimalFormat f = new DecimalFormat("##.00"); 
      DecimalFormat n = new DecimalFormat("##"); 

      float votesForPolly; 
      float votesForErnest; 
      float totalPolly = 0; 
      float totalErnest = 0; 
      String response; 
      int precinctsforpolly = 0; 
      int precinctsforernest = 0; 
      int precinctsties = 0; 

      Scanner scan = new Scanner(System.in); 
      System.out.println();  
      System.out.println ("Election Day Vote Counting Program"); 
      System.out.println(); 

      do 
      { 
       //point A 
       System.out.println("Do you wish to enter more votes? Enter y:n"); 
       response = scan.next(); 
       if (response.equals("y")) 
       { 
        //point B 
        System.out.println("Enter votes for Polly:"); 
        votesForPolly = scan.nextInt(); 
        System.out.println("Enter votes for Ernest:"); 
        votesForErnest = scan.nextInt(); 

        totalPolly = totalPolly + votesForPolly; 
        totalErnest = totalErnest + votesForErnest; 


         System.out.println("Do you wish to add precincts? Enter y:n"); 
         response = scan.next(); 
         while (response.equals("y")) 
         { 
          System.out.println("How many precincts voted for Polly: "); 
          precinctsforpolly = scan.nextInt(); 
          System.out.println("How many precincts votes for Ernest: "); 
          precinctsforernest = scan.nextInt(); 
          System.out.println("How many were ties: "); 
          precinctsties = scan.nextInt(); 

          break; 
          //not returning to point A, instead it returns to point B 
         } 
          if (response.equals("n")) 
          { 
           break; 

          } 


        if (response.equals("n")) 
        { 
         break; 

        } 
       } 
      } 
      while (response.equals("n")); 
      System.out.println("Final Tally"); 
      System.out.println("Polly received:\t " + n.format(totalPolly) + " votes\t" + f.format((totalPolly/(totalPolly + totalErnest))*100) + "%\t" + precinctsforpolly + " precincts"); 
      System.out.println("Ernest received: " + n.format(totalErnest) + " votes\t" + f.format((totalErnest/(totalPolly + totalErnest))*100) + "%\t" + precinctsforernest + " precincts"); 
      System.out.println("\t\t\t\t\t" + precinctsties + " precincts tied"); 

     } 
} 

我的猜測是,該字符串回覆已經確定要在循環這就是爲什麼它跳過第一步,跳右後衛進入循環假設我的答案已經y的結束年。

+0

'我一直在試圖弄清楚這一點,在過去6個hours'一樣嚴重? –

+0

是什麼讓你覺得循環不回到頂端?你給了什麼投入,你得到了什麼答案? –

+1

嘗試更改最外層「而」條件'while(response.equals(「y」));' – davedwards

回答

0

你只需要去掉不必要的break s的代碼,並且還可以處理2個不同的響應。當響應是y時,你的循環也應該繼續,而不是相反。

見編輯下面的代碼:

import java.util.Scanner; 
import java.text.DecimalFormat; 

public class Election 
{ 
    public static void main (String[] args) 
    { 

     DecimalFormat f = new DecimalFormat("##.00"); 
     DecimalFormat n = new DecimalFormat("##"); 

     float votesForPolly; 
     float votesForErnest; 
     float totalPolly = 0; 
     float totalErnest = 0; 
     String response; 
     int precinctsforpolly = 0; 
     int precinctsforernest = 0; 
     int precinctsties = 0; 

     Scanner scan = new Scanner(System.in); 
     System.out.println();  
     System.out.println ("Election Day Vote Counting Program"); 
     System.out.println(); 

     do 
     { 
      //point A 
      System.out.println("Do you wish to enter more votes? Enter y:n"); 
      response = scan.next(); 
      if (response.equals("y")) 
      { 
       //point B 
       System.out.println("Enter votes for Polly:"); 
       votesForPolly = scan.nextInt(); 
       System.out.println("Enter votes for Ernest:"); 
       votesForErnest = scan.nextInt(); 

       totalPolly = totalPolly + votesForPolly; 
       totalErnest = totalErnest + votesForErnest; 


        System.out.println("Do you wish to add precincts? Enter y:n"); 
        String response2 = scan.next();//create a new response variable, so the loop doesn't confuse which response to break on 
        //I removed the inner loop. If you really intended for this to be an inner loop then the while(response2.equals("y")){} should be around the question, not after 
        if (response2.equals("y")) 
        { 
         System.out.println("How many precincts voted for Polly: "); 
         precinctsforpolly = scan.nextInt(); 
         System.out.println("How many precincts votes for Ernest: "); 
         precinctsforernest = scan.nextInt(); 
         System.out.println("How many were ties: "); 
         precinctsties = scan.nextInt(); 

        } 
       //removed the unnecessary checks for when response in 'n' 
      } 
     } 
     while (response.equals("y")); 
     System.out.println("Final Tally"); 
     System.out.println("Polly received:\t " + n.format(totalPolly) + " votes\t" + f.format((totalPolly/(totalPolly + totalErnest))*100) + "%\t" + precinctsforpolly + " precincts"); 
     System.out.println("Ernest received: " + n.format(totalErnest) + " votes\t" + f.format((totalErnest/(totalPolly + totalErnest))*100) + "%\t" + precinctsforernest + " precincts"); 
     System.out.println("\t\t\t\t\t" + precinctsties + " precincts tied"); 

    } 
} 
0
if (response.equals("n")) 
{ 
    break; 
} 

中斷while循環。

如果用戶的回答是'y',您可能需要再次循環。
在這種情況下,使用while (response.equals("y"));

0

您的要求就是打出來,如果用戶在用戶輸入「Y」,但你的代碼表示,進入「N」,但環,

do{ 
..... 
}while (response.equals("n")); 

它應該是

while (response.equals("y")); 
0
 do { 
      //point A 
      System.out.println("Do you wish to enter more votes? Enter y:n"); 
      response = scan.next(); 
      if (response.equals("y")) 
      { 
       //point B 
       #code 
       if (response.equals("n")) //<--- this 'if' block will break the loop when response equals 'n' 
       { 
        break; 
       } 
      } 
     } 
     while (response.equals("n"));// <----- this means doing loop when response equals "n" 

你可以看到我的上述代碼塊註釋,循環會做一個更多的時間,只有當響應等於「N」。但是當響應等於'n'時,其上方的'if'塊將打破循環,並且while (response.equals("n"))將不被檢查。