Results 1 to 13 of 13
Thread: Please Help!!!
- 03-05-2011, 06:03 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
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.
- 03-05-2011, 06:07 PM #2
Member
- Join Date
- Feb 2011
- Posts
- 20
- Rep Power
- 0
Don't use
useJava Code:if (n1>n2) and (n1>n3)
and use [ CODE ][ /CODE ]Java Code:if((n1>n2) && (n1>n3))
- 03-05-2011, 06:10 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Your if statements seem incorrect.
You could use math.max however through, what happens if you nest them?Java Code:if(condition1 && condition2){ //do something }
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.
- 03-05-2011, 06:15 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
Hi
Thanks for the help.
Will try it and see if it works
Thanks alot
- 03-05-2011, 06:16 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
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??
- 03-05-2011, 06:18 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 67
- Rep Power
- 0
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:
Java 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:
Java 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 ;)
- 03-05-2011, 06:26 PM #8
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
@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...
- 03-05-2011, 06:29 PM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
So you are looking for a c++, tutorial. This is different than java, they are two entirely different languages.
- 03-05-2011, 06:33 PM #10
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
Yes, a C++ Tutorial.
- 03-05-2011, 06:39 PM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
Last edited by sunde887; 03-05-2011 at 06:43 PM.
-
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.
- 03-05-2011, 07:05 PM #13
Member
- Join Date
- Feb 2011
- Posts
- 20
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks