2015-10-05 34 views
-1

所以我介紹到Java的過程中,我應該創建一個使用數組,看起來像這樣一個簡單的高爾夫評分程序:高爾夫成績理貨程序的Java - 卡住

(我卡上時,底部它涉及到我所有的高爾夫球得分和統計陣列的結果......)

你打了多少洞? 9

Hole Pars

孔1的標準是什麼? 5

孔2的標準是什麼? 4

洞3的標準是什麼? 4

你的成績

你是怎麼在孔1拍? 4

你在洞2上射了什麼? 5

你在洞3上射了什麼? 3


你的得分摘要包括:

總數:帕

總孔一體:0

雙鷹總數(信天翁):0

鷹總數:0

總數小鳥的:5

總數收杆的:1

轉向架的總數:1

雙轉向架的總數:在孔的2

總數3個或更多高於標準桿:0

這是我不得不遠代碼,

/* Lab 4: Part 2 - Golf Score */ 

import java.io.*; 
import java.text.*; 
import java.util.Scanner; 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.IOException; 

public class golf_score2 
{ 
public static void main(String[] args) throws IOException 
{ 

    Scanner m= new Scanner(System.in); 
    Scanner q= new Scanner(System.in); 

    BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 

    System.out.print("How many holes did you play?"); 

    int h = m.nextInt(); 
    int hArray[] = new int[h]; 
    int pArray[] = new int[h]; 

    // Asks the user to enter par for i number of holes 

    System.out.print("\nHole Pars"); 

    for(int i=0;i <h;i++) 
    { 
    System.out.print("\n\nWhat is par for hole " + (i+1) + "? "); 
    hArray[i]=m.nextInt(); 
    } 

    // Asks the user to enter score for i holes 

    System.out.print("\n\nYour Scores"); 

    for(int i=0; i <h;i++) 
    { 
    System.out.print("\n\nWhat did you shoot on hole " + (i+1) + "? "); 
    pArray[i]=m.nextInt(); 
    } 

    System.out.print("\n\n---------------------------"); 
    System.out.print("\n\nYour scoring summary includes:"); 

    score_result = pArray[i] - hArray[i]; 

    System.out.print(score_result); 

} 
} 
+0

看看這個答案:http://stackoverflow.com/questions/24187709/getting-golf-scores-into-an-array?rq = 1使用2維數組。 – jsh

+0

這是一個提示。這是對你的類的最後一部分的改變:'for(int i = 0; i

+0

所以我認爲我爲「score_result」添加的最後一部分是完全錯誤的。我喜歡jsh所說的關於嘗試二維數組的問題,但我可能必須完全重新格式化我的代碼。基本上,每個洞的總得分應該是(洞洞分數 - 孔洞面值)。我想計算總數,我只是不知道是否有可能使用上面的代碼。有什麼建議麼? –

回答

0

看起來你需要使用另一個for循環,以確保你清點每個分數:

for(int i = 0; i < h; i++) { 
    score_result = pArray[i] - hArray[i]; 
    System.out.print(score_result); 
} 

如果你只是想最後的比分會是這個樣子:

int final_score = 0; 
for(int i = 0; i < h; i++) { 
    score_result = pArray[i] - hArray[i]; 
    final_score += score_result; 
} 
System.out.println(final_score); 
+0

由於某些原因,它仍然不起作用,我知道這是正確的軌道,但也許有什麼錯誤的事實,我使用兩個陣列......?我很確定我的「score_result」類是錯誤的,我仍然不知道如何解決計算以獲得最終得分。 –

+0

你應該很好地使用兩個數組 - 你正在跟蹤兩個不同的東西(面值和分數)。 2D數組更適用於數據表。 –

+0

扎卡里,你給的代碼吐出了幾個錯誤,在編譯過程中,不知道在哪裏從: –