2017-05-26 83 views
1

我正在創建一個具有不同方法的骰子游戲。但是,我似乎無法弄清楚如何在我的方法之間傳遞變量以用於其他方法。它從一個菜單開始,用戶將「選擇」奇數1或2,然後轉到滾動方法以獲得滾動的總和,然後計算方法告訴用戶他們是否有一個或丟失。但是,我似乎無法弄清楚如何傳遞diceSum並在計算方法中使用我的方法之間進行選擇。在方法之間傳遞變量

我似乎無法弄清楚如何讓select和diceSum在diceCalc()中用於打印出用戶的答案。

private static void guess(){ 
    Scanner input = new Scanner(System.in); 
    System.out.print("(1) Odd\n" 
      +"(2) Even\n" 
      +"(3) Quit Game\n" 
      +"What is your selection: "); 
    try { 
     int select = input.nextInt(); 
     if(select == 1) { 
      System.out.println(""); 
      diceRoll(); 
     } else if(select == 2) { 
      System.out.println(""); 
      diceRoll(); 
     } else if(select == 3) { 
      System.out.println("Thank you for playing Dueling Dice v1.0..."); 
    system.out(0); 
} 

private static void diceRoll() { 
    int roll1, roll2, diceSum; 
    roll1 = (int)(Math.random()*6+1); 
    roll2 = (int)(Math.random()*6+1); 
    diceSum = roll1 + roll2; 

    System.out.println(" Dice 1 roll = " + roll1); 
    System.out.println(" Dice 2 roll = " + roll2); 
    System.out.println("TOTAL dice roll = " + diceSum); 
    diceCalc(); // this is called in the main after the guess since guess performs the roll 
} 

private static void diceCalc() { 
    if (diceSum % 2 != 0 && select == 1 || diceSum % 2 == 0 && select == 2) { 
     if (select == 1) { 
      System.out.println("Dice rolled = " + diceSum + "and you selected odd"); 
     } else { 
      System.out.println("Dice rolled = " + diceSum + "and you selected even"); 
     } 
     System.out.println("CONGRATULATIONS! You WIN!!"); 
     won++; 
     played++; 
    }else if (diceSum % 2 == 0 && select == 1 || diceSum % 2 != 0 && select == 2){ 
     if (select == 1) { 
      System.out.println("Dice rolled = " + diceSum + "and you selected odd"); 
     } else { 
      System.out.println("Dice rolled = " + diceSum + "and you selected even"); 
     } 
     System.out.println("I am sorry you lost!"); 
     lost++; 
     played++; 
    } 
    System.out.print("it is making it here!"); 
+0

如何打印特定的字符串?你將它作爲println()方法的參數傳遞,對吧?那麼,你如何做diceCalc應該有權訪問diceSum?它不應該將它作爲參數,以便調用diceCalc()的diceRoll()可以將它作爲參數傳遞? –

+0

您是否設法解決問題? – luizfzs

回答

1

您可以將diceSum傳遞給diceCalc方法。

diceCalc(diceSum); // this is called in the main after the guess since guess performs the roll 

然後將參數添加到diceCalc函數。

private static void diceCalc(int diceSum) 
0

您可以使用一些在Java中讀取函數和參數。 Check here

儘管這一點,你可以這樣做:

通行證select參數diceRoll方法

private static void guess(){ 
    int select = <someValue> 
    ... 
    diceRoll(select); 
    ... 
} 

diceRoll方法,你聲明參數如下圖所示。

private static void diceRoll(int varSelect) { 
    ... 
    System.out.println("Select value: " + varSelect); 
    ... 
} 

編輯1:只要記住,原始類型的Java按值傳遞的,這意味着如果你改變內部diceRollvarSelect的價值,它不會範圍更新select值方法guess。檢查這個問題:Is Java 「pass-by-reference」 or 「pass-by-value」?

0

下面是例子:

void methodA(){ 
int toB = 5; 
methodB(toB); 
} 
void methodB(int fromA){ 
int recievedFromA = fromA; 
System.out.println(recievedFromA); 
} 

什麼書或課程您使用的? 我不知道你是在學習編程的最基本概念之前寫一些控制檯遊戲的。如果你自學自我,那麼我建議你找一些像Joshua Bloch的Effective Java這樣的好書,來幫助你學習最基本和最先進的概念。

0

方法中聲明的變量(例如diceSum)僅對該特定方法是本地的。其他方法無法訪問它們。

public static void main(String[] args) { 
    int diceSum; // Can only be used in this method 
} 

public static void diceCalc() { 
    // The variable diceSum is not visible here, it can only be accessed 
    // by the main() method 
} 

但是,您可以將參數傳遞給方法。你只需將它們放在括號之間:

public static void main(String[] args) { 
    int diceSum = 3; 
    diceCalc(diceSum); 
} 

public static void diceCalc(int diceSum) { 
    // diceSum will contain the value 3 
    ... 
} 

這使事情變得更加動態一件好事。例如,diceCalc()方法取決於它是奇數還是偶數的菜單項號。如果您想要更改菜單項(或者創建一個圖形用戶界面,用戶單擊按鈕以選擇奇數還是偶數),則還必須更改方法diceCalc()

0

謝謝你的幫助,但想通了。我知道我必須將我的方法更改爲int來返回參數,並且它完美地工作。

private static int diceRoll() { 
    int roll1, roll2, diceSum; 
    roll1 = (int)(Math.random()*6+1); 
    roll2 = (int)(Math.random()*6+1); 
    diceSum = roll1 + roll2; 

    System.out.println(" Dice 1 roll = " + roll1); 
    System.out.println(" Dice 2 roll = " + roll2); 
    System.out.println("TOTAL dice roll = " + diceSum); 
    return diceSum; 

} 

private static void diceCalc() { 
    if (diceSum % 2 != 0 && select == 1 || diceSum % 2 == 0 && select == 2) { 
     if (select == 1) { 
      System.out.println("\nDice rolled = " + diceSum + " and you selected odd"); 
     } else { 
      System.out.println("\nDice rolled = " + diceSum + " and you selected even"); 
     } 
     System.out.println("CONGRATULATIONS! You WIN!!"); 
     won++; 
     played++; 
    }else if (diceSum % 2 == 0 && select == 1 || diceSum % 2 != 0 && select == 2){ 
     if (select == 1) { 
      System.out.println("Dice rolled = " + diceSum + "and you selected odd"); 
     } else { 
      System.out.println("Dice rolled = " + diceSum + "and you selected even"); 
     } 
     System.out.println("I am sorry you lost!"); 
     lost++; 
     played++; 
    } 
} 
0

它在你的情況下更好地申報Dice類,然後定義你的方法是骰子

成員

還要定義selectdiceSum作爲類的成員變量骰子

之後,它會是簡單的在類Dice的任何成員方法中訪問這些變量

以這種方式,您的程序將更好地組織和易於閱讀。

public class Dice { 

    private int select; 
    private int diceSum; 

    private void guess(){ 
    Scanner input = new Scanner(System.in); 
    System.out.print("(1) Odd\n" 
      +"(2) Even\n" 
      +"(3) Quit Game\n" 
      +"What is your selection: "); 

     select = input.nextInt(); 
     if(select == 1) { 
      System.out.println(""); 
      diceRoll(); 
     } else if(select == 2) { 
      System.out.println(""); 
      diceRoll(); 
     } else if(select == 3) { 
      System.out.println("Thank you for playing Dueling Dice v1.0..."); 
     } 
    } 
    private void diceRoll() { 
    int roll1, roll2; 
    roll1 = (int)(Math.random()*6+1); 
    roll2 = (int)(Math.random()*6+1); 
    diceSum = roll1 + roll2; 

    System.out.println(" Dice 1 roll = " + roll1); 
    System.out.println(" Dice 2 roll = " + roll2); 
    System.out.println("TOTAL dice roll = " + diceSum); 
    diceCalc(); // this is called in the main after the guess since guess performs the roll 
    } 

    private void diceCalc() { 
    if (diceSum % 2 != 0 && select == 1 || diceSum % 2 == 0 && select == 2) { 
     if (select == 1) { 
      System.out.println("Dice rolled = " + diceSum + "and you selected odd"); 
     } else { 
      System.out.println("Dice rolled = " + diceSum + "and you selected even"); 
     } 
     System.out.println("CONGRATULATIONS! You WIN!!"); 
     won++; 
     played++; 
    }else if (diceSum % 2 == 0 && select == 1 || diceSum % 2 != 0 && select == 2){ 
     if (select == 1) { 
      System.out.println("Dice rolled = " + diceSum + "and you selected odd"); 
     } else { 
      System.out.println("Dice rolled = " + diceSum + "and you selected even"); 
     } 
     System.out.println("I am sorry you lost!"); 
     lost++; 
     played++; 
    } 
    System.out.print("it is making it here!"); 
    } 
}