-
Please Help!!!
Hi
I am trying to compare 3 values and output the largest.
I tried the methods in the Math class, but it did not work as it only compared 2 values- i.e the "Math.max(x,y)" one. Now im trying to compare them using a if statement but have run into a dead end...
Help and a solution would be much appreciated..
here is my code:
import java.util.Scanner;
public class ExB_8
{
public static void main(String[]args)
{
Scanner scan = new Scanner (System.in);
double n1, n2, n3;
System.out.println("Enter number 1:");
n1= scan.nextDouble();
System.out.println("Enter number 2:");
n2= scan.nextDouble();
System.out.println("Enter number 3:");
n3= scan.nextDouble();
if (n1>n2) and (n1>n3)
System.out.println("Largest number is:"+ n1);
if (n2>n1) and (n2>n3)
System.out.println("Largest number is:"+ n2);
if (n3>n2) and (n3>n1)
System.out.println("Largest number is:"+ n3);
jGRASP: operation complete.
ÏÏÏÏ
ÏÏ«Ï ----jGRASP exec: javac -g ExB_8.java
ÏϧÏ
ϼ§ÏExB_8.java:20: ';' expected
ÏÏ§Ï if (n1>n2) and (n1>n3)
ÏÏ§Ï ^
ϼ§ÏExB_8.java:23: ';' expected
ÏÏ§Ï if (n2>n1) and (n2>n3)
ÏÏ§Ï ^
ϼ§ÏExB_8.java:26: ';' expected
ÏÏ§Ï if (n3>n2) and (n3>n1)
ÏÏ§Ï ^
ÏϧÏ3 errors
ÏϧÏ
ÏÏ§Ï ----jGRASP wedge2: exit code for process is 1.
ÏÏ©Ï ----jGRASP: operation complete.
-
Don't use
Code:
if (n1>n2) and (n1>n3)
use
Code:
if((n1>n2) && (n1>n3))
and use [ CODE ][ /CODE ]
-
Your if statements seem incorrect.
Code:
if(condition1 && condition2){
//do something
}
You could use math.max however through, what happens if you nest them?
Also, a more reusable approach is to store the inputs in an array and use a loop and an if statement to determine the largest number.
-
Hi
Thanks for the help.
Will try it and see if it works
Thanks alot
-
By the way, does anyone know where i can get a good, free, C++ Java Tutorial for beginners, something thats comprehensive, yet easy to follow...
Thanks again
-
What is a "C++ Java Tutorial"?
Are you looking for a C++ tutorial?
A Java tutorial?
One combined??
-
I don't think "and" is a keyword in Java, what you are looking for is the logical and operator which is two ampersands (&&). You also need to place all the comparisons inside of a set of brackets so your if statements would look more like:
Code:
if ((n1>n2) && (n1>n3))
You can also use a call to the math function as a parameter of another call to the math function like the following to compare 3 values:
Code:
maxValueOfXYZ = Math.max(Math.max(x,y),z);
Hope that helps :)
Edit:
Damn I'm a slow typist, to much revising and checking what I say ;)
-
@Fubarable
I'm very new to Programming and my teacher is teaching us in C++. But he hasnt gone through all the basics, like "What is a class or method" etc. Instead, he teaches us how to translate Pseudo Code to C++, so i want something that can guide me through the basics...
-
So you are looking for a c++, tutorial. This is different than java, they are two entirely different languages.
-
-
This is a java forum so you may not get much help with that question. However; I will suggest cprogramming.com, or, if you have a textbook for your class, read it.
-
You are in the wrong forum as this is a Java forum. I think that you would do well to Google for a C++ forum.
-