Im trying to generate a random number and operator for every instance : "System.out.println (firstNumbereasy + operatorSwitch + secondNumbereasy) but the same operator and number is being generated each time.
My main method is
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);{
my code for generating random number and operator is
Random randN = new Random();
int firstNumbereasy; //declaring variables for easy mode
int secondNumbereasy;
firstNumbereasy = randN.nextInt(11) + 1;
secondNumbereasy= randN.nextInt(firstNumbereasy - 1);
Random operatorChoice = new Random();
int operator = operatorChoice.nextInt(3);
int b = (0);
int c = (0);
String operatorSwitch;
switch (operator){// generates random operators
case 0: operatorSwitch= "+";
c = (int) (firstNumbereasy+secondNumbereasy);
break;
case 1: operatorSwitch= "-";
c = (int) (firstNumbereasy-secondNumbereasy);
break;
case 2: operatorSwitch= "*";
c = (int) (firstNumbereasy*secondNumbereasy);
break;
case 3: operatorSwitch= "/";
c = (int) (firstNumbereasy/secondNumbereasy);
default: operatorSwitch="";
}
but when i try to print it with :
int selection;
System.out.println("-----------------------");
System.out.println("Choose a mode");// displays mode user can pick
System.out.println("1. Easy");
System.out.println("2. Hard");
System.out.println("3. Easy Elimination");
System.out.println("4. Hard Elimination");
selection = scanner.nextInt(); // reads what mode user picks
switch(selection) {
case 1:
System.out.println("Question 1, what is " + firstNumbereasy +operatorSwitch+ secondNumbereasy );
b= scanner.nextInt();
System.out.println("Question 2, what is " + firstNumbereasy + operatorSwitch + secondNumbereasy);
b = scanner.nextInt();
case 2:
The same random number and operator for every time i use :System.out.println("Question 1 what is " +firstNumbereasy + operatorSwitch + secondNumbereasy);
is the same
The reason for your problem is that you're generating the values of firstNumbereasy, secondNumbereasy and operator only once in your code, if you'd like to have them changed for each question you need to re-run the following lines of code every time:
int firstNumbereasy = randN.nextInt(11) + 1;; //declaring variables for easy mode
int secondNumbereasy = randN.nextInt(firstNumbereasy - 1);
int operator = operatorChoice.nextInt(3);
my personal suggestion would be to extract this to a function as it's going to repeat a lot it seems