最近開始計算機編程,我被困在家庭作業上。我創建了一個循環,但不是回到頂端,而是從我打算的位置開始提前一步。 (這一切都在我的編碼提出的意見列出。我一直在試圖弄清楚這一點,過去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的結束年。
'我一直在試圖弄清楚這一點,在過去6個hours'一樣嚴重? –
是什麼讓你覺得循環不回到頂端?你給了什麼投入,你得到了什麼答案? –
嘗試更改最外層「而」條件'while(response.equals(「y」));' – davedwards