Link to home
Create AccountLog in
Avatar of jtiernan2008
jtiernan2008

asked on

Can someone please help me with this simple java program

The question is to enter 3 numbers and find which is the larger of them but it will not run
Can someone please take a look at it.
import java.util.Scanner;
 
public class Largest {
	
	public static void main (String args[]) {
		
		Scanner input = new Scanner();
		
		int number1, number2, number3, weight1 = 0, weight2 = 0, weight3 = 0;
		
		System.out.println("Please enter the first number");
		number1 = input.nextInt();
		System.out.println("Please enter the second number");
		number2 = input.nextInt();
		System.out.println("Please enter the third number");
		number3 = input.nextInt();
		
		if ((number1 > number2) || (number 1 > number3)) {
			System.out.println("number 1 is the largest number");
		}
		
		if ((number2 > number1) || (number2 > number3)) {
			System.out.println("number 2 is the largest number");
		}
		
		if ((number3 > number 1) || (number3 > number1)) {
			System.out.println("number 3 is the largest number")
		}
		
		
		
	}
}

Open in new window

SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
just a few typos really, and you neeed to pass System.in to the Scanner ctor

ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of jtiernan2008
jtiernan2008

ASKER

@ mahome

so why cannot I not use || which is 'or'?

Would it not mean one or the other or am I not understanding the context?
or try this one
import java.util.*;
 
public class Largest {
 
    public static void main(String args[]) {
 
        Scanner input = new Scanner(System.in);
        int maxNumber, number1, number2, number3,
                weight1 = 0, weight2 = 0, weight3 = 0;
        String MaxNumberName = "";
        System.out.println("Please enter the first number");
        number1 = input.nextInt();
        System.out.println("Please enter the second number");
        number2 = input.nextInt();
        System.out.println("Please enter the third number");
        number3 = input.nextInt();
 
        if (number1 > number2) {
            maxNumber = number1;
            MaxNumberName = "number1";
        } else {
            maxNumber = number2;
            MaxNumberName = "number2";
        }
        if (maxNumber < number3) {
            maxNumber = number3;
            MaxNumberName = "number3";
        }
        System.out.println(MaxNumberName + " is the largest number");
 
    }
}

Open in new window

U are right but if u want find largest number ur condition is not correct