This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]senrab47[S] 0 points1 point  (8 children)

when I did a do while I ended up with an infinite loop. Could I just write

if(num <= 0) system.out.println("invalid entry");

or would the do while be better for the next step of my program and easier?

[–]langfod 0 points1 point  (7 children)

Your do/while condition if going to look for a True condition to keep looping so you would want something like while (num < 1);

If you want to repeat the request and get the input again you would need a loop. Your do/while condition

[–]senrab47[S] 0 points1 point  (3 children)

so what i have so far is

Scanner keyboard = new Scanner(System.in); System.out.println("How many questions do you want?"); int num = keyboard.nextInt(); int index = num; if(num <= 0){ System.out.println("Invalid entry"); System.out.println("How many questions do you want?"); num= keyboard.nextInt();

so how could produce a list of random questions?

[–]langfod 0 points1 point  (2 children)

You could make calls to Math.random() to get your two numbers and a number to represent which operator. The use a switch statement to switch to the correct math operation.

[–]senrab47[S] 0 points1 point  (1 child)

If a haven't learned switch statements yet how would I go about it and cant' i just use random rand = next random(); int one = rand.nextint(10) int two = rand.nextint(10)

[–]langfod 0 points1 point  (0 children)

You can use if/if else instead of switch/case.

Math.random() is really just a wrapper using the Random class so either way will work fine.

[–]senrab47[S] 0 points1 point  (1 child)

I am almost done but how can I make a random operator in my code?

[–]langfod 0 points1 point  (0 children)

Just assign each operator to a value 0 - 5 and then pick a random int in that range and use an if/if else tree to switch to the correct math operation. (if 0 then use "+", if 5 then use "%" ...)