我的問題是當控制檯打印結果;該程序似乎正在增加所有的結果,我似乎無法弄清楚如何重置每一個失去它。 我很確定問題在於for循環,但我仍然不知道如何解決它。如何重置for循環而不輸出爲前面的「擊球手」輸入的信息?
下面是完整的代碼:
import java.util.*;
import java.text.NumberFormat;
public class BattingAverageApp
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
double successfulRuns = 0;
double totalBases = 0;
int atbatt = 0;
String message = " ";
AtBat bat = new AtBat();
System.out.print("Number of batters: ");
int batters = sc.nextInt();
System.out.println();
bat.setPlayers(batters);
int[] atbat = new int[batters];
double[][] calculate = new double[batters][2];
outerLoop:
for(int a = 0; a < batters; a++)
{
atbat[a] = Validator.getInt(sc, "Enter the number of times at bat for player "
+ (a+1) + ": ", 0, 11);
System.out.println();
atbatt = atbat[a];
double[] results = new double[atbatt];
System.out.println("0 = out, 1 = single, 2 = double, 3 = triple, 4 = home run");
for(int i = 0; i < atbatt; i++)
{
results[i] = 0;
results[i] = Validator.getInt(sc, "Results for at-bat " + i + ": ", -1, 5);
bat.setResult(results[i]);
if (results[i] > 0)
successfulRuns++;
totalBases += results[i];
}
System.out.println();
}
for(int a = 0; a < batters; a++)
{
calculate[a][0] = successfulRuns/(double)atbatt;
calculate[a][1] = totalBases/(double)atbatt;
NumberFormat number = NumberFormat.getNumberInstance();
number.setMinimumFractionDigits(3);
message = "Batting average: " + number.format(calculate[a][0]) + "\t"
+ "Slugging percent: " + number.format(calculate[a][1]);
System.out.println(message);
}
System.out.println();
}
}
這裏是什麼控制檯打印出,當我選擇一個玩家:
Number of batters: 1
Enter the number of times at bat for player 1: 3
0 = out, 1 = single, 2 = double, 3 = triple, 4 = home run
Results for at-bat 0: 0
Results for at-bat 1: 4
Results for at-bat 2: 0
Batting average: 0.333 Slugging percent: 1.333
Press any key to continue . . .
,這裏是什麼它打印出來,當我選擇多個玩家:
Number of batters: 2
Enter the number of times at bat for player 1: 3
0 = out, 1 = single, 2 = double, 3 = triple, 4 = home run
Results for at-bat 0: 0
Results for at-bat 1: 4
Results for at-bat 2: 0
Enter the number of times at bat for player 2: 2
0 = out, 1 = single, 2 = double, 3 = triple, 4 = home run
Results for at-bat 0: 0
Results for at-bat 1: 1
Batting average: 1.000 Slugging percent: 2.500
Batting average: 1.000 Slugging percent: 2.500
Press any key to continue . . .
究竟什麼不行?你有沒有例外?另外,'玩家'是什麼意思? –
你可以通過查看我們之間的區別2案例(選擇1麪糊,並選擇更多)明確。 –
當我回家時,我會發布其餘的代碼。 – ednomx