This site requires JavaScript, please enable it in your browser!
Greenfoot back
dangwang
dangwang wrote ...

2014/6/14

number input and string output

dangwang dangwang

2014/6/14

#
im trying to write a program where it asks user for 3 number inputs. if all numbers are equal it will display "all equal". if its not equal then it will display "not equal" i can make it work with 2 numbers but with 3 numbers, its kinda tricky. any help is appreicaited here is my code so far
    int number1 = 0;
    int number2 = 0;
    int number3 = 0;

    public void run() 
    {   
        number1 = readInt("Enter number #1 :");
        number2 = readInt("Enter number #2 :");
        number3 = readInt("Enter number #3 :");
        
        if (number1 == number2)
            System.out.println("All numbers are equal");
        else if  (number1 == number2 == false)
            System.out.println("Numbers are not equal");
    }
if i add all 3 number variables to the if statement i get an error. i tried using the logical operators but found no solution.
danpost danpost

2014/6/14

#
Line 13 could simply be 'else' as you are asking the opposite of what is asked in line 11. Line 11 could be:
if (number1 == number2 && number2 == number3)
for comparing all three numbers.
dangwang dangwang

2014/6/14

#
sorry forgot to add the if 2 numbers are the same then it would print "two numbers are the same" thanks for the help! you can close this thread :)
You need to login to post a reply.