我正在创建一个具有不同方法的骰子游戏。但是,我似乎无法弄清楚如何在我的方法之间传递变量以用于其他方法。它从一个菜单开始,用户将“选择”奇数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