Results 1 to 13 of 13

Thread: Please Help!!!

  1. #1
    Avthar is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Exclamation 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.

  2. #2
    YAY
    YAY is offline Member
    Join Date
    Feb 2011
    Posts
    20
    Rep Power
    0

    Default

    Don't use
    Java Code:
    if (n1>n2) and (n1>n3)
    use
    Java Code:
    if((n1>n2) && (n1>n3))
    and use [ CODE ][ /CODE ]

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your if statements seem incorrect.
    Java 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.

  4. #4
    Avthar is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    Hi

    Thanks for the help.
    Will try it and see if it works

    Thanks alot

  5. #5
    Avthar is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    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

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  7. #7
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    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 ;)

  8. #8
    Avthar is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    @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...

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    So you are looking for a c++, tutorial. This is different than java, they are two entirely different languages.

  10. #10
    Avthar is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    Yes, a C++ Tutorial.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  13. #13
    YAY
    YAY is offline Member
    Join Date
    Feb 2011
    Posts
    20
    Rep Power
    0

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •