最近コンピュータのプログラミングが開始され、宿題が残っています。私はループを作りましたが、先頭に戻るのではなく、私が意図したところから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であることを決定してきました。
'私は真剣のように、過去6 hours'のためにこれを把握しようとしています? –
ループが一番上に戻っていないと思いますか?どのようなインプットを与えていますか、どのような反応を得ていますか? –
最も外側の "while"条件を変更しようとするwhile(response.equals( "y")); ' – davedwards