I am creating a dice game that has different methods. However, I cannot seem to figure out how to pass variables between my methods for use in the other method. It starts with a menu where the user will "select" either 1 for odd or 2 for even then goes to the roll method to get the sum of the roll and then the calculation method to tell the user whether they have one or loss. However, I cannot seem to figure out how to pass diceSum and select between my methods for use in the calculations method.
I cannot seem to figure out how to get select and diceSum to be used in the diceCalc() to print out the answer for the user.
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!");