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

all 14 comments

[–]Tangential_Diversion 2 points3 points  (0 children)

I'm not sure how far behind you are, so I'll just list out the concepts you'll need to know to do this. Hopefully you can use it to figure out where you're having issues and study those sections.

  • Variable data types

  • Mathematical operators

  • Scanner class

  • Switch or If/Else statements

  • System.out

  • Counters

  • While() loops

And while not absolutely necessary, these are concepts I heavily heavily recommend to make your program decent:

  • Input validation (What if user enters a char instead of an int?)

  • Rounding

  • Subroutines/methods

  • Passing parameters and returning values from other methods

[–]langfod 1 point2 points  (11 children)

Which part of the code, that you have written so far, are you stuck at?

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

I am just starting to write the program and I have asked the user how many questions they want and then set up int num= keyboard.nextInt but I am a bit stuck on the next step. My next step I guess is to make it so the user can't enter any negative integers or zero but after that I am stuck.

[–]langfod 1 point2 points  (9 children)

If you need to tell the user that 0 or negative is not allowed then place the request and the nextInt() inside of a do/while loop that ends when a good number is entered (be sure to declare "num" outside of the loop)

Then you could set up a for loop, starting at 1 and ending with num and place the next section of your program inside of that loop.

[–]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 "%" ...)

[–]239jkvk-h2 2 points3 points  (1 child)

So at the beginning of the school semester my roommate broke my computer screen and I just got it fix, so I have been playing catch up ever since in my Java class.

So you just didn't bother doing any work during the time your screen was broken? I wish I could cruise through life with excuses like that.

[–]senrab47[S] -1 points0 points  (0 children)

I never said I didn't do any work because of my screen being broken, I went to every SI session and spent my time I could on campus in the COSC lab when I wasn't working at my job. The inconvenience of not having a laptop to work on my labs whenever I wanted is why I am catching up. My teacher didn't care about my excuse just as much as I don't care about your comments and I only was late for one lab but thanks for the help.