2014-05-03 46 views
-1

我的問題是當控制檯打印結果;該程序似乎正在增加所有的結果,我似乎無法弄清楚如何重置每一個失去它。 我很確定問題在於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 . . . 
+0

究竟什麼不行?你有沒有例外?另外,'玩家'是什麼意思? –

+0

你可以通過查看我們之間的區別2案例(選擇1麪糊,並選擇更多)明確。 –

+0

當我回家時,我會發布其餘的代碼。 – ednomx

回答

0

它可能是由於這些行:

calculate[a][0] = successfulRuns/(double)atbatt; 
calculate[a][1] = totalBases/(double)atbatt; 

您只投了分母。但提名者也是一個整數。您必須將所有變量加倍。用這些更改行:

calculate[a][0] = (double)((double)successfulRuns/(double)atbatt); 
calculate[a][1] = (double)((double)totalBases/(double)atbatt); 
+0

您正在施放整數除法的結果。 –

+0

@ PM77-1你說得對,我改變了他們。 – padawan

+0

@cagirici沒有。除了atbatt之外,它們都是雙重的。就像我在我的問題上說過的(對不起,如果沒有這樣做),如果我只選擇了一個0,4,0(1個本壘打,兩個輸出)作爲結果的擊球手,平均和猛擊打印出來沒問題; .333和1.333就像它應該。 – ednomx

0

這似乎是你沒有清除舊值的每個循環迭代到你的setResult方法。我不能肯定地說,因爲你還沒有發佈你的AtBat類的代碼,但我會看看這種方法。

+0

我該如何去清除它們?我只是有一個set和get方法,這個方法是一個帶有in參數的void方法,get是一個帶有set參數返回值的靜態方法。 – ednomx

+0

您可以在類中創建一個新方法,將實例變量重置爲0,並在循環結束時調用它,以便可以重新開始。你是否嘗試過使用各種輸入而不是每次輸入3次?怎麼了? – nrj4life

+0

我做了,我剛剛更新了顯示控制檯在蝙蝠不同的問題。我也嘗試過一種方法,它不起作用,也許我錯誤地使用了它;我會再次嘗試使用該方法。 – ednomx

0

好吧,我想通了。我只需要移動我的公式if(results [a]> 0) successfulRuns ++;總計基數+ =結果[a];在循環內部之外。

現在它的工作很完美。感謝那些試圖提供幫助的人。

 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[a] > 0) 
      successfulRuns++; 
      totalBases += results[a]; 

     System.out.println(); 

    calculate[a][0] = successfulRuns/(double)atbatt; 
    calculate[a][1] = totalBases/(double)atbatt; 
    } 

    for(int a = 0; a < batters; a++) 
    { 
    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); 
    }