# Thread: Set user input equal to correct?

1. Member
Join Date
Sep 2016
Posts
7
Rep Power
0

## Set user input equal to correct?

Hello, I am fairly new to java and am almost finished with my program assignment, but need help with little part. The user is supposed to input an answer which equals number1 - number2. (These numbers are random) The problem I am having is that the answer may be correct but sometimes it still says incorrect as well as sometimes it will display number-number1. Also If the answer is incorrect I am suppose to display the correct answer( Which I have figured out). The bigger number is suppose to subtract the smaller number. Thanks.

Java Code:
```package Labs;
// Allows the use of Scanner utility for user input
import java.util.Scanner;
import java.util.Random;
public class Lab4 {

public static void main(String[] args) {
// Declare variables
int number1 = (int) (Math.random() * 10);
int number2 = (int)	(Math.random() * 10);
int usernum = 0;
Scanner keyboard = new Scanner (System.in);

if(number1 >= number2)
{
System.out.println("What is: " + number1 + " - " +number2 );
usernum = keyboard.nextInt();
usernum = (number1 - number2);
System.out.println("Correct");
}

if  (number1 <= number2);
{
System.out.println("What is: " + number2 + " - " +number1);
usernum = keyboard.nextInt();
usernum = (number2 - number1);
System.out.println("Incorrect");
System.out.println(number2 - number1);		}

}

}```

2. ## Re: Set user input equal to correct?

user is supposed to input an answer which equals number1 - number2.
Assign the value of the subtraction to a variable and compare that variable with the user's input.

Also posted at: https://coderanch.com/t/670905/java/...ul-subtraction

3. Member
Join Date
Sep 2016
Posts
7
Rep Power
0

## Re: Set user input equal to correct?

Thanks. The only problem I have now is the not equal to operator. It keeps saying invalid operator and I have no clue what to do

package Labs;
// Allows the use of Scanner utility for user input
import java.util.Scanner;
import java.util.Random;
public class Lab4 {

public static void main(String[] args) {
// Declare variables
int number1 = (int) (Math.random() * 10);
int number2 = (int) (Math.random() * 10);
int usernum1;
int usernum2;
Scanner keyboard = new Scanner (System.in);

if(number1 >= number2)
{
System.out.println("What is: " + number1 + " - " +number2 );
usernum1 = keyboard.nextInt();
usernum1= (number1 - number2);
System.out.println("Correct");
}

else if(number2 >= number1);
{
System.out.println("What is: " + number2 + " - " +number1);
usernum2 = keyboard.nextInt();
usernum2 = (number2 - number1);
System.out.println("Correct");
usernum2 != (number2 - number1);
System.out.println(number2 - number1);
}

}

}

4. ## Re: Set user input equal to correct?

the not equal to operator
The operator is: !=

It keeps saying invalid operator
Please copy the full text of the error message and paste it here. It has important info about the error.

Also be sure to wrap your code in code tags.

5. Member
Join Date
Sep 2016
Posts
7
Rep Power
0

## Re: Set user input equal to correct?

Yes, but it for some reason will not work. The full error says syntax error on token "!=", Invalid assignment operator. Here is the code below.

Java Code:
```package Labs;
// Allows the use of Scanner utility for user input
import java.util.Scanner;
import java.util.Random;
public class Lab4 {

public static void main(String[] args) {
// Declare variables
int number1 = (int) (Math.random() * 10);
int number2 = (int)	(Math.random() * 10);
int usernum1;
int usernum2;
Scanner keyboard = new Scanner (System.in);

if(number1 >= number2)
{
System.out.println("What is: " + number1 + " - " +number2 );
usernum1 = keyboard.nextInt();
usernum1= (number1 - number2);
System.out.println("Correct");
}

else if(number2 >= number1);
{
System.out.println("What is: " + number2 + " - " +number1);
usernum2 = keyboard.nextInt();
usernum2 = (number2 - number1);
System.out.println("Correct");
usernum2 != (number2 - number1);
System.out.println(number2 - number1);
}

}

}```

6. ## Re: Set user input equal to correct?

"!=", Invalid assignment operator.
That is correct. != is the NOT EQUAL operator. It is used to compare two values and return a true or false value.
The assignment operator is =

You forgot to post the full text of the error message that includes the line number where the error is (above the ^). For example:
Java Code:
```TestCode22.java:1022: error: not a statement
1 != 2;
^```
Last edited by Norm; 09-29-2016 at 03:01 AM.

7. Member
Join Date
Sep 2016
Posts
7
Rep Power
0

## Re: Set user input equal to correct?

So how would I write usernum2 is not equal to (number2 - number1)?

8. ## Re: Set user input equal to correct?

how would I write usernum2 is not equal to (number2 - number1)?
Line 29 does exactly that.

What should the code as a result of that comparison? There needs to be a statement that uses the results (true or false) of the comparison.

Look at lines 15 and 23. Both those lines have a comparison that the if statements use to control the execution of other statements.

9. Member
Join Date
Sep 2016
Posts
7
Rep Power
0

## Re: Set user input equal to correct?

I have been doing this for a couple of hours and have tried everything. This is so stressful. I don't know how you can do this.

10. ## Re: Set user input equal to correct?

Look at lines 15 and 23.

Study how to write if statements: http://docs.oracle.com/javase/tutori...dbolts/if.html

11. Member
Join Date
Sep 2016
Posts
7
Rep Power
0

## Re: Set user input equal to correct?

Thanks for the help, but I'm just going to submit my program like this. I give up

12. ## Re: Set user input equal to correct?

Be sure to ask your instructor to show you how to code an if statement.

13. Member
Join Date
Sep 2016
Posts
7
Rep Power
0

## Re: Set user input equal to correct?

lol could you please just give me the answer. I did the rest on my own?

14. ## Re: Set user input equal to correct?

Try writing an if statement like you already have done on lines 15 and 23.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•