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.
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")
}
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
just a few typos really, and you neeed to pass System.in to the Scanner ctor
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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");
}
}
U are right but if u want find largest number ur condition is not correct