我正在創建一個具有不同方法的骰子游戲。但是,我似乎無法弄清楚如何在我的方法之間傳遞變量以用於其他方法。它從一個菜單開始,用戶將「選擇」奇數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!");
如何打印特定的字符串?你將它作爲println()方法的參數傳遞,對吧?那麼,你如何做diceCalc應該有權訪問diceSum?它不應該將它作爲參數,以便調用diceCalc()的diceRoll()可以將它作爲參數傳遞? –
您是否設法解決問題? – luizfzs