2013-04-15 65 views
0

好的,所以,我對java很新。
我正在設計一個分數計算器,這個計劃對於我一直擱在一旁的項目來說。不過,我想知道如何做到這一點。得分計算器

該程序應該要求擲骰子,並將其添加到前面的每個玩家。
我假設一個while循環會完成這個任務,但是每次它通過循環時,都會將變量重置爲當前的卷。因此,我不能讓一個總...

下面是一些代碼:

static int players; 
    static String p1; 
    static String p2; 
    static String p3; 
    static String p4; 
    static int maxScore; 
    static int roll1; 
    static int roll2; 
    static int roll3; 
    static int roll4; 
    static int total1; 
    static int total2; 
    static int total3; 
    static int total4; 
    public static void main(String[] args) { 
    Scanner keyboard = new Scanner(System.in); 
    System.out.print("Enter number of players: "); 
    players=keyboard.nextInt(); 
    System.out.print("Enter Maximum Score: "); 
    maxScore=keyboard.nextInt(); 
    if(players==2){     //will add more ifs when i get the code right 
     System.out.println("Please enter players names."); 
     System.out.print("Player 1: "); 
     p1=keyboard.next(); 
     System.out.print("Player 2: "); 
     p2=keyboard.next(); 
     System.out.println(p1 + "\t \t " + p2 + "\n" 
     + "_______ \t _______ \n"); //displays scorecard look with players names 

     { 


     while (total1 < maxScore && total2<maxScore) { 
     //scorecard quits when maxScore is reached by either player 
     int roll; 
     total1=(roll+roll1); 

     System.out.print(""); 
     roll=keyboard.nextInt(); //asks for current roll 

     System.out.print("\n"+"_____"+"\n"); 
     System.out.print(roll+"+"+"\n"+roll1+"\n"+"_____"+"\n"+(roll+roll1)+"\n"); 
     /*i want this to display total score + last roll and then 
     *total it again on the next line*/ 
     roll1=roll; 
     } 
+1

看起來像是將'roll1'分配給'roll'而不是添加其值。你可以試試:'roll1 + = roll;' – jcern

+1

while循環之前的大括號是什麼?什麼變量應該保持總量?總數是1嗎?如果是這樣,只需將卷裝入卷1,然後執行總計+ = roll1(+ =表示總數=總計+卷) – Ben313

回答

1

在Java編程進度的一些提示:

  1. 變量roll沒有任何意義。如roll1等等,將存儲每個玩家的最後一卷。

  2. 如果可能,初始化您的變量。應該避免依賴默認值,因爲它可能會在學習中給您帶來問題(NullPointerException會在某個時間訪問您)。

  3. 在你的循環中,你有total1=(roll+roll1);。這是錯誤的。您的變量total1,roll,roll1在程序到達此點時未進行初始化。因爲它們是整數,所以它們(默默)初始化爲0,所以total1在這一點上產生0,這並沒有太大的成就。在此之後,您繼續檢索卷。嘗試另一種方式,先卷,然後合起來。

  4. 你提到你是Java的新手,但是在將來某個時候,你可能會考慮在數組中實現同樣的程序。您會注意到它可以節省您現在編寫的大量重複代碼。

總結,並翻譯成代碼準則(2個玩家):

public class MyScoreCalculator { 
    static String p1 = ""; 
    static String p2 = ""; 
    static int maxScore = 0; 
    static int roll1 = 0; 
    static int roll2 = 0; 
    static int total1 = 0; 
    static int total2 = 0; 

    public static void main(String[] args) { 
     Scanner keyboard = new Scanner(System.in); 
     // Dialogue to get data... 
     // Display scorecard look with players names 

     while (total1 < maxScore && total2 < maxScore) { 
      //scorecard quits when maxScore is reached by either player 
      roll1 = keyboard.nextInt(); // ask for current roll 

      System.out.println(total1 + "+"); 
      System.out.println(roll1); 
      System.out.println("_____"); 
      System.out.println(roll1 + total1); 

      total1 = total1 + roll1; 

      // Do the same for next player. 
     } 
    } 
} 
+0

謝謝。我終於意識到我在循環中過早地定義了總數。我知道我需要學習很多東西。這是我的第一個編程語言,我只待了一個星期。感謝您的幫助 – derek

+0

@derek緩慢而穩定,這就是每個人在學習編程時都會如何去做的。這沒什麼錯。我指出的第四個暗示遲早會在你的學習路線中顯現出來,以及查爾斯在他的回答中提出的建議,雖然對於初學者來說這是一些更高級的東西。如果這些答案中的任何一個解決了您的問題,請不要忘記接受它。 – afsantos

1

如果我正確地讀你的問題,然後解決的辦法是

total1+=(roll+roll1); 

這是同樣的事情as

total1= total1+(roll+roll1); 

您只是不將卷加入總價值!

同樣值得注意的是,將實例變量設置爲公共和靜態並不是一個好主意。如果他們是私人的而不是靜態的,會更好。例如

private int players; 

希望的答案可以幫助

+0

感謝您的答覆。在我原來的代碼中,我確實已經添加到下一卷。我必須以某種方式將它排除在外。我遇到的問題是,當它顯示總數時,顯示'0' – derek

0

你共1計算應total1 += roll和新的軋輥輸入後發生。如果roll1表示最後一個卷,則相應地命名該變量,它的可讀性更高。

由於您有很多玩家,請嘗試抽象概念並將輸入和「會計」分開。例如,您可以創建一個包含總數和最後一個輸入(以及玩家名稱)的PlayerScore類,並使用一種方法來負責添加和保存最後一次輸入以及打印相應的信息。然後,您可以收集PlayerScore並迭代它,詢問當前的滾動並更新信息。