2015-07-10 30 views
-1

我曾經幫助編寫這個程序作爲骰子游戲的模擬,但由於語法/括號的情況,我似乎無法讓它運行。我想我在課堂上組織我的方法時遇到了麻煩。有小費嗎?在模擬骰子游戲的課堂中組織類的方法

import java.util.Random; 

public class diceGame 
{ 
    private class DiceRoll 
    { 
     public static Random rng = new Random(); 
     private int a, b, c; //rolls 

     public DiceRoll() 
     { 
      this.a = rng.nextInt(6); 
      this.b = rng.nextInt(6); 
      this.c = rng.nextInt(6); 
     } 

     public int getScore() 
     { 
      if (a ==6 && b == 6 && c ==6) 
      { 
       return 500; 
       System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!"); 
      } 
      else if ((a == 6 && b == 6 && b != c) || (b == 6 && c == 6 && c !=a)) 
      { 
       return 100; 
       System.out.println("Congratulations! You rolled two 6s! You just earned 100 points!!"); 
      } 
      else if ((a == b && b != c) || (b == c && c !=a)) 
      { 
       return 50; 
       System.out.println("Congratulations! You just earned 50 points!!"); 
      } 
      else 
      { 
       System.out.println("Nice Try!"); 
      } 
     } 

     public String toString() 
     { 
      return String.format("Rolled a %d, %d, and %d", a, b, c); 
     } 
    } 

    public static void main (String args[]) 
    { 
     DiceRoll d; 
     while (true) 
     { 
      d = new DiceRoll(); 
      d.getScore();  // gives you how much to change the player score 
     } 
     // finish client 
    } 
} 
} 
+3

/我嘆了口氣......你能_請_格式化你的代碼嗎?請? –

+0

最後還有一個'}'。 – Koshinae

+2

您應該使用像eclipse這樣的IDE來幫助您修復錯誤。 – user2718281

回答

1

你在你的代碼中的多個問題

  1. 有一些方法沒有return語句
  2. 有返回後幾個系統輸出的(這使得代碼可達)
  3. 的概念父類內部類是錯誤的。在內部類中,說到靜態,你可以只有靜態和最終的常量。
  4. 包圍是錯誤的。

下面的代碼將工作,只有當所有的骰子被卷6

import java.util.Random; 

public class diceGame { 
    private class DiceRoll { 
     public Random rng = new Random(); 
     private int a, b, c; // rolls 

     public DiceRoll() { 
      this.a = rng.nextInt(6); 
      this.b = rng.nextInt(6); 
      this.c = rng.nextInt(6); 
     } 

     public int getScore() { 
      if (a == 6 && b == 6 && c == 6) { 
       System.out 
         .println("Congratulations! You rolled three 6s! You just earned 500 points!!"); 
       return 500; 

      } else if ((a == 6 && b == 6 && b != c) 
        || (b == 6 && c == 6 && c != a)) { 
       System.out 
         .println("Congratulations! You rolled two 6s! You just earned 100 points!!"); 
       return 100; 

      } else if ((a == b && b != c) || (b == c && c != a)) { 
       System.out 
         .println("Congratulations! You just earned 50 points!!"); 
       return 50; 

      } else { 
       System.out.println("Nice Try!"); 
       return 0; 
      } 
     } 

     public String toString() { 
      return String.format("Rolled a %d, %d, and %d", a, b, c); 
     } 
    } 

    public static void main(String args[]) { 
     DiceRoll d; 
     while (true) { 
      d = new diceGame().new DiceRoll(); 
      if (d.getScore() == 500) { // gives you how much to change the player score 

       break; 
      } 

     } 
     // finish client 
    } 
} 

您將需要解決所有這些對骰子滾動終止。

+1

並非所有的方法都需要返回!如果你沒有返回,它會運行所有的代碼,直到方法結束 –

+0

@Alyssa,你有沒有嘗試上面的代碼? –

2

一看:

if (a ==6 && b == 6 && c ==6){ 
    return 500; 
    System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!"); //This is the error! 
} 

一旦你從方法返回,任何其他代碼不會被運行,就會造成編譯錯誤。我認爲這是你想要什麼:

if (a ==6 && b == 6 && c ==6) { 
    System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!"); 
    return 500; 
} 

而且,看看這個:

public static void main(String args[]) { 
    DiceRoll d; 
    while (true) { 
    d = new DiceRoll(); 
    d.getScore(); // gives you how much to change the player score 
    } // finish client 
} 
} 
} //Extra 

刪除標 「額外」 的花括號。

此外,隨機rng是靜態的,但它是在非靜態類中聲明的。

將其更改爲private static class DiceRoll

getScore()方法borked;它不會返回任何東西。

public int getScore() { 
     if (a == 6 && b == 6 && c == 6) { 
      return 500; 
      System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!"); 
     } else if ((a == 6 && b == 6 && b != c) || (b == 6 && c == 6 && c != a)) { 
      return 100; 
      System.out.println("Congratulations! You rolled two 6s! You just earned 100 points!!"); 
     } else if ((a == b && b != c) || (b == c && c != a)) { 
      return 50; 
      System.out.println("Congratulations! You just earned 50 points!!"); 
     } else { 
      System.out.println("Nice Try!"); 
      //Wut no return? 
     } 
    } 

你可能想:

public int getScore() { 
    if (a == 6 && b == 6 && c == 6) { 
     System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!"); 
     return 500; 
    } else if ((a == 6 && b == 6 && b != c) || (b == 6 && c == 6 && c != a)) { 
     System.out.println("Congratulations! You rolled two 6s! You just earned 100 points!!"); 
     return 100; 
    } else if ((a == b && b != c) || (b == c && c != a)) { 
     System.out.println("Congratulations! You just earned 50 points!!"); 
     return 50; 
    } else { 
     System.out.println("Nice Try!"); 
     return -1; 
    } 
    } 

而且它是一個好主意,打電話給你的diceGameDiceGame

+0

這個修復本身並不會使骰子運行。 –

+1

@KalyanChavali只是指出這一事實 –

+1

現在編輯,包含更多信息 –

1

沒有您的代碼比較少的問題。第一個是關於使用內部類的想法,所以內部類應該出去。所有的方法都可以進入外部類。將滾動放入構造函數會使遊戲無聊,a,b和c在實例的生命週期中保持不變,或者強制您爲每個滾動創建新實例。真的很糟糕的設計決策。我會將其移至getScore。 最後while(true)將永遠持續下去,你將不得不殺死程序來阻止它。這裏是你的代碼和檢查得分邏輯,我不知道規則:

import java.util.Random; 

public class diceGame { 
    public static Random rng = new Random(); 
    private int a, b, c; //rolls 
    public int getScore() { 
    this.a = 1 + rng.nextInt(5); 
    this.b = 1 + rng.nextInt(5); 
    this.c = 1 + rng.nextInt(5); 
    if (a == 6 && b == 6 && c == 6) { 
     System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!"); 
     return 500; 
    } else if ((a == 6 && b == 6 && b != c) || (b == 6 && c == 6 && c != a)) { 
     System.out.println("Congratulations! You rolled two 6s! You just earned 100 points!!"); 
     return 100; 
    } else if ((a == b && b != c) || (b == c && c != a) || (a == c && b != a)) { 
     System.out.println("Congratulations! You just earned 50 points!!"); 
     return 50; 

    } else { 
     System.out.println("Nice Try!"); 
     return 0; 
    } 
    } 
    public String toString() { 
    return String.format("Rolled a %d, %d, and %d", a, b, c); 
    } 
    public static void main(String args[]) { 
    diceGame d = new diceGame(); 
    for (int i = 0; i < 3; i++) { 
     System.out.println(d.getScore()); 
     System.out.println(d.toString()); 
    } 
    } 
}