2017-04-22 13 views
-1

最近コンピュータのプログラミングが開始され、宿題が残っています。私はループを作りましたが、先頭に戻るのではなく、私が意図したところから1ステップ先に出発します。 (それはすべて私がコーディングでのコメントに記載されています。私は、誰かが私を助けることができるならば、それは非常に高く評価しまうので、過去6時間のためにこれを把握しようとしている。Do/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とすると仮定すると、右バックループにジャンプする理由であるループの最後でYであることを決定してきました。

+0

'私は真剣のように、過去6 hours'のためにこれを把握しようとしています? –

+0

ループが一番上に戻っていないと思いますか?どのようなインプットを与えていますか、どのような反応を得ていますか? –

+1

最も外側の "while"条件を変更しようとするwhile(response.equals( "y")); ' – davedwards

答えて

0

不要なコードを取り除くだけで、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」の場合、ユーザーの応答がある場合は、再度ループすることがあります。01その場合 、あなたの要件は、それがなければならないユーザの入力が「Y」ですが、あなたのコードは言う、

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

ながら、ユーザが「N」が、ループに入った場合に抜け出すことですwhile (response.equals("y"));

0

を使用あなたはループ応答が「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'に等しいとき、上の 'if'ブロックはループを破り、while (response.equals("n"))はチェックされません。

関連する問題