Stupid question about Java

Ok, had a quick look at your code above.. there are several reasons why it won't compile.
First problem with 'else without if' is due to a rogue semi-colon here (highlighted in red):
else if (usersmallnumber > computersmallnumber && computergen2 > computergen1); {
System.out.println("Hurray! You have won! Hurray! :-)");

Also as stated you've initialised Scanner twice.. finally your last three 'else if' statements require capital 'S' on 'System.out.println'.. otherwise it won't recognise the package, due to being case sensitive.
system.out.println("UnluckyXXXXXXXXXXXXX - You have lost! :-(");
 
Yeah the above compiles for me fine. It's because before the edit you had: 'else if {' So you missed the 'if clause'. That's probably why errors were thrown.

You can do it without braces, like: 'if (isMoving) currentSpeed--;' but missing out the if clause or putting it within the 'then' statement will cause errors.

Hope that helps!
 
Does it actually work? Because it's doing the opposite of what I want it to do :/ (I'm actually just sitting here laughing at it now...possibly swearing at it as well...)
 
Does it actually work? Because it's doing the opposite of what I want it to do :/ (I'm actually just sitting here laughing at it now...possibly swearing at it as well...)

No it doesn't work... It keeps saying I've lost and entered an invalid number ..but had a quick look at the code and realised why.

You have stated that if the users number does NOT equal 1 OR 2 then print the error 'they have input an invalid number'. So it is ALWAYS true. As when it equals 1 it DOES NOT equal 2 and visa versa!
When really you want it to say: if a user input does NOT equal 1 AND does NOT equal 2 then display the error. Otherwise continue with the game!

So if you change this line:
if (usersmallnumber != 1 || usersmallnumber != 2) {
to:
if (usersmallnumber != 1 && usersmallnumber != 2) {

Then after that it seems to work fine. ;)
 
What why?

If a user chooses the number '1' say.
Using OR, your program states that if the input does not equal '1' (which it does - true/1) OR does not equal '2' (which it DOESN'T - false/0) then display the error message.

Soo if you relate this back to an OR truth table (or binary logic):

A B O
0 0 0
0 1 1
1 0 1
1 1 1

O = output, A & B just stand for inputs.
So as you can see your if clause is satisfied and so the 'then' clause runs.
Where as if you change it to && and input '1', then if the input does NOT equal '1' (which it does - true/1) AND does not equal '2' (which it DOESN'T - false/0) then display the error message.

Now an AND truth table looks like the following:

A B O
0 0 0
1 0 0
0 1 0
1 1 1

I've highlighted what your result would look like. So because the if statements output is '0' (or false) then the if clause IS NOT satisfied and the program will continue to run.

Hope this helps, in some way! ;)
 
Back
Top Bottom