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

1. Member Join Date
Sep 2014
Posts
5
Rep Power
0

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

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;

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);
}
}  Reply With Quote

2. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
26

## 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'?  Reply With Quote

3. Member Join Date
Sep 2014
Posts
5
Rep Power
0

## 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.  Reply With Quote

4. Member Join Date
Sep 2014
Posts
5
Rep Power
0

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

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;

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);
}
}```  Reply With Quote

5. Member Join Date
Sep 2014
Posts
5
Rep Power
0

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

Hopefully the above makes it easier to read.  Reply With Quote

6. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
26

## 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)'?  Reply With Quote

7. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
26

## 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.  Reply With Quote

8. Member Join Date
Sep 2014
Posts
5
Rep Power
0

## 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?  Reply With Quote

9. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
26

## 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.  Reply With Quote

#### Posting Permissions

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