I am having trouble keeping track of a variable that is assigned by a random number generator. I am trying to generate two random numbers with different ranges and based on the numbers generated with update the score or don't and then tell the user if they win or loss based on a set of rules.
I have attached the code below so you can see what I am talking about but I basically need help with the variables roll1 and roll2, thanks for the help.
import java.util.Random; // import random number class
import java.util.Scanner; // Import the Scanner class
public class Main
{
public static void main(String[] args) {
//rules and variables
System.out.println("Welcome to lucky dice, the computer will roll 2 dice...");
//int roller1 = (int )(Math.random() * 10 + 1); //instance of random class
//int roller2 = (int )(Math.random() * 20 + 2); //instance of random class
//Random rand = new Random();
int total = 0;
int num = 0;
int max = 11;
int min = 1;
int max2 = 21;
int min2 = 2;
//create a loop to keep rolling until the user wins or looses
while (num <= 20 && total >= -50 && total < 150){
System.out.println("Are you ready to roll?");
Scanner reply = new Scanner(System.in); // Create a Scanner object
String answer = reply.nextLine();
Random rand = new Random();
if (answer == "yes"){
int roll1 = rand.nextInt(max - min) + min;//instance of random clas
int roll2 = rand.nextInt(max2 - min2) + min2;//instance of random clas
}while(roll2 % 2 == 1){
int roll2 = rand.nextInt(max2 - min2); //instance of random clas
}if (roll1 > roll2 || roll2 > roll1){
total = total + roll1 + roll2; //keep track of score and tell user
System.out.printf("Your roll was: " + roll1, roll2 + ".");
System.out.printf("Your score is now " + total + ".");
num = num + 1;
}else if (roll1 == roll2){
total = total + roll1 + roll2; //keep track of score and tell user
System.out.printf("Your roll was: " + roll1, roll2 + ".");
System.out.printf(" Your score is now " + total + ". ");
num = num + 1;
}
}if (num > 20){
System.out.println("You lost!");
}else if (total >= 150){
System.out.println("You won!");
}else if (total >= -50){
System.out.println("You lost!");
}
}
}