Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Tolls

Thread: I'm having an issue with my max value in my code... any help?

  1. #1
    cprice48 is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default I'm having an issue with my max value in my code... any help?

    I'm using Jgrasp and my prompt is asking me to write a program where the program prompts the user to enter 4 grades between 0 and 100. And you have to display the grades, and the program has to calculate the minimum grade, the maximum grade and the average.
    PS: this may be stupid, but I'm new to java. haha
    I'm just having problems with the max, any help?

    // Import Java Scanner.
    import java.util.Scanner;

    // Name Class.
    public class Practice_4_1
    {
    public static void main(String[] args)
    {

    int a, b, c, d, min = 0, high = 0;
    Scanner input = new Scanner(System.in);

    // Prompt user to enter four grades between 0 and 100.
    System.out.println("Enter four grades between 0 and 100: ");
    a = input.nextInt();
    b = input.nextInt();
    c = input.nextInt();
    d = input.nextInt();

    // Calculate minimum grade.
    if (a < b)
    if (a < c)
    if (a < d)
    min = a;
    else
    min = d;
    else
    if (b < c)
    if (b < d)
    min = b;
    else
    min = d;
    else
    if (c < d)
    min = c;
    else
    min = d;

    // Calculate the highest grade.
    if (a > b)
    if (a > c)
    if (a > d)
    high = a;
    else
    high = d;
    else
    if (b > c)
    if (b > d)
    high = b;
    else
    high = d;
    else
    if (c > d)
    high = c;
    else
    high = d;

    // Calculate the average.
    int average = ( a + b + c + d ) / 4;


    // Conclusions.
    System.out.println("The grades entered are: " + a + " , " + b + " , " + c + " and " + d );
    System.out.println("The minimum grade entered is: " + min );
    System.out.println("The maximum grade entered is: " + high);
    System.out.println("The average of the grades is: " + average);
    }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: I'm having an issue with my max value in my code... any help?

    When posting code here please wrap it in [code] tags [/code] so it retains its formatting and is readable.

    What problem are you having?
    You have code to handle 'min', so what trouble are you having with 'max'?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    cprice48 is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default Re: I'm having an issue with my max value in my code... any help?

    oh, okay! thanks I did not know that.
    And every time I compile it and run it, it runs just fine but I'll enter numbers like "60, 70, 80, and 90" and it will give me my minimum, but it tells me my max is 100, when obviously it's supposed to say "90".
    So I don't know what I'm doing wrong.

  4. #4
    cprice48 is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default Re: I'm having an issue with my max value in my code... any help?

    Java Code:
    // Import Java Scanner.
    import java.util.Scanner;
    
    // Name Class.
    public class Practice_4_1
    {
       public static void main(String[] args)
       {
       
          int a, b, c, d, min = 0, high = 0;
          Scanner input = new Scanner(System.in);
       
       // Prompt user to enter four grades between 0 and 100.
          System.out.println("Enter four grades between 0 and 100: ");
          a = input.nextInt();
          b = input.nextInt();
          c = input.nextInt();
          d = input.nextInt();
       
       // Calculate minimum grade.
          if (a < b)
             if (a < c)
                if (a < d)
                   min = a;
                else 
                   min = d;
             else
                if (b < c)
                   if (b < d)
                      min = b;
                   else
                      min = d;
                else
                   if (c < d)
                      min = c;
                   else
                      min = d;
       
       // Calculate the highest grade.
          if (a > b)
             if (a > c)
                if (a > d)
                   high = a;
                else 
                   high = d;
             else
                if (b > c)
                   if (b > d)
                      high = b;
                   else
                      high = d;
                else
                   if (c > d)
                      high = c;
                   else
                      high = d;
       
       // Calculate the average.
          int average = ( a + b + c + d ) / 4;
       
       
       // Conclusions.
          System.out.println("The grades entered are: " + a + " , " + b + " , " + c + " and " + d );
          System.out.println("The minimum grade entered is: " + min );
          System.out.println("The maximum grade entered is: " + high);
          System.out.println("The average of the grades is: " + average);
       }
    }

  5. #5
    cprice48 is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default Re: I'm having an issue with my max value in my code... any help?

    Hopefully the above makes it easier to read.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: I'm having an issue with my max value in my code... any help?

    You have aproblem with min as well.
    Try entering 90,80,70,60...in that order.

    It's the same problem with your max.

    If you look at the structure code, where is the else for 'if (a < b)'?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: I'm having an issue with my max value in my code... any help?

    This is a case where {} would help to define the structure of your if/else statements.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    cprice48 is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default Re: I'm having an issue with my max value in my code... any help?

    Oh, you're right.
    And do you mean the very first one?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: I'm having an issue with my max value in my code... any help?

    I generally stick {} around all my if/else and loops so I don't have to think...:)
    This should then show you your logic.
    Norm likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 7
    Last Post: 09-02-2014, 02:38 PM
  2. Code formatting Issue
    By camickr in forum Suggestions & Feedback
    Replies: 21
    Last Post: 10-08-2011, 04:19 PM
  3. Replies: 8
    Last Post: 06-06-2011, 02:27 AM
  4. Issue compiling Java Code
    By AggressiveFish in forum Java Applets
    Replies: 4
    Last Post: 01-04-2011, 09:10 PM
  5. Replies: 4
    Last Post: 03-14-2009, 10:36 AM

Posting Permissions

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