Results 1 to 14 of 14
  1. #1
    Join Date
    Sep 2016
    Posts
    7
    Rep Power
    0

    Default 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. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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
    If you don't understand my response, don't ignore it, ask a question.

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

    Default 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. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default 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. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Set user input equal to correct?

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

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default 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. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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
    If you don't understand my response, don't ignore it, ask a question.

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

    Default 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. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Set user input equal to correct?

    Be sure to ask your instructor to show you how to code an if statement.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Set user input equal to correct?

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

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Set user input equal to correct?

    Try writing an if statement like you already have done on lines 15 and 23.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 16
    Last Post: 05-01-2014, 12:54 PM
  2. x input string is not equal to "x"
    By monkeynote in forum New To Java
    Replies: 3
    Last Post: 01-18-2012, 07:45 AM
  3. User input
    By the ole buc in forum New To Java
    Replies: 16
    Last Post: 12-11-2011, 07:08 PM
  4. Help with user input
    By sconniegorilla in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 02:00 PM
  5. Replies: 21
    Last Post: 06-11-2009, 02:18 AM

Posting Permissions

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