# Thread: Need Help in my First Java Program

1. Member
Join Date
Nov 2014
Posts
10
Rep Power
0

## Need Help in my First Java Program

hey sorry if it is the right place to post .
I have been trying all day to find solution and i am discouraged.

here we go: i need to calculate the minimum and the maximum, actually it seems to be easy but, the minimum should be the smallest number but 0

this is my code

Java Code:

Scanner s = new Scanner (System.in);

int max = 0 ;
int min = 0 ;

System.out.println(" Please enter 3-5 numbers");
int a = s.nextInt();
int b = s.nextInt();
int c = s.nextInt();
int d = s.nextInt();
int e = s.nextInt();

if  (a>b && a>c && a>d && a>e)
max = a;
else if (b>a && b>c &&b>d &&b>e)
max = b;
else if (c>b && c>a &&c>d &&c>e)
max = c;
else if (d>a && d>b &&d>c &&d>e)
max = d;
else
max = e;

when the user enters '5','23','12','0','0'
the minimum should be 5 and not 0

Last edited by elias2903; 11-21-2014 at 06:31 PM.

2. ## Re: Need Help in my First Java Program

The code appears to require the user to enter 5 numbers, but asks the user for 3-5?

Do you know how to write a loop? A loop could be used to repeatedly ask the user for the next number and to compare the number entered against the last number entered instead of always requiring 5 numbers be entered.

Please edit your post and wrap your code with code tags:
[code]
YOUR CODE GOES HERE
[/code]
to get highlighting and preserve formatting.

3. Member
Join Date
Nov 2014
Posts
10
Rep Power
0

## Re: Need Help in my First Java Program

i know how to write loops but the teacher asked to use only if statement
i am asking the user to enter 3-5 numbers . he could write as well
0
0
3
5
6
i need to know what is the minimum using "if statement"
i was trying to do as i did with the maximum but the answer was " the minimum is '0' " instead of '3'

4. ## Re: Need Help in my First Java Program

Where is the code that tests for the minimum value not equal to 0?

5. Member
Join Date
Nov 2014
Posts
10
Rep Power
0

## Re: Need Help in my First Java Program

Originally Posted by Norm
Where is the code that tests for the minimum value not equal to 0?
sorry if i wasnt clear wnought.
the question goes like this.
" ask the user 3 prices minimum and 5 prices maximum, if there is no price the user should enter '0' .
finally calculate the average EXCLUDING the maximum and the minimum value "

example :

0 2200 2300 1990 1900

the minimum is 1900
the maximum is 2300
the avg is (1990+2200)/2

my only problem is that i dont know how to get the minimum number.
it keeps showing tha it is '0' . i am limited to use if statement only

I am new to java and sorry for lack of knowledge

6. ## Re: Need Help in my First Java Program

i dont know how to get the minimum number.
Where is the code that you are having problems with?

The logic will be very similar to the code to find the max.

7. Member
Join Date
Nov 2014
Posts
10
Rep Power
0

## Re: Need Help in my First Java Program

Originally Posted by Norm
Where is the code that you are having problems with?

The logic will be very similar to the code to find the max.

Java Code:

int max = 0 ;
int min = 0 ;

System.out.println(" Please enter 3-5 numbers");
int a = s.nextInt();
int b = s.nextInt();
int c = s.nextInt();
int d = s.nextInt();
int e = s.nextInt();

if (a>b && a>c && a>d && a>e)
max = a;
else if (b>a && b>c &&b>d &&b>e)
max = b;
else if (c>b && c>a &&c>d &&c>e)
max = c;
else if (d>a && d>b &&d>c &&d>e)
max = d;
else
max = e;

if (a<b && a<c && a<d && a<e)
min = a;
else if (b<a && b<c &&b<d &&b<e)
min = b;
else if (c<b && c<a &&c<d &&c<e)
min = c;
else if (d<a && d<b &&d<c &&d<e)
min = d;
else
min = e;

System.out.println(min);

8. ## Re: Need Help in my First Java Program

Where is the test for 0?

9. Member
Join Date
Nov 2014
Posts
10
Rep Power
0

## Re: Need Help in my First Java Program

Originally Posted by Norm
Where is the test for 0?
what do you mean by test for 0 ?
how should i do it ?

10. ## Re: Need Help in my First Java Program

I thought you did NOT want a min value that was 0.
Use the == or != operator to test if a variable has a 0 value.

11. Member
Join Date
Nov 2014
Posts
10
Rep Power
0

## Re: Need Help in my First Java Program

Originally Posted by Norm
I thought you did NOT want a min value that was 0.
Use the == or != operator to test if a variable has a 0 value.
Thanks for your quick replies.
i dont see how it helps me
all i want is to have the minimum number that is not 0

if the user enters 0 0 1 2 3
the minimum according my code will show " 0 "
and i want the code to show "1" ( the smallest number excluding 0 )

12. ## Re: Need Help in my First Java Program

minimum number that is not 0
Use (number != 0) to make that test

13. Member
Join Date
Nov 2014
Posts
10
Rep Power
0

## Re: Need Help in my First Java Program

Originally Posted by Norm
Use (number != 0) to make that test
inside the if statement?
should i create new variable ?

I tried this one , i added &&a!=0
but it still not working
Java Code:
if ((a<b && a<c && a<d && a<e)&&a!=0)
min = a;
else if ((b<a && b<c &&b<d &&b<e)&&b!=0)
min = b;
else if ((c<b && c<a &&c<d &&c<e)&&c!=0)
min = c;
else if ((d<a && d<b &&d<c &&d<e)&&d!=0)
min = d;
else
min = e;

14. ## Re: Need Help in my First Java Program

it still not working
What if one of the variables is 0? Then none of the other variables will be less than it.

What is the value of e? It is not tested.

15. Member
Join Date
Nov 2014
Posts
10
Rep Power
0

## Re: Need Help in my First Java Program

Originally Posted by Norm
What if one of the variables is 0? Then none of the other variables will be less than it.

What is the value of e? It is not tested.
Java Code:
if ((a<b && a<c && a<d && a<e)&(a!=0))
min = a;
else if ((b<a && b<c &&b<d &&b<e)&(b!=0))
min = b;
else if ((c<b && c<a &&c<d &&c<e)&(c!=0))
min = c;
else if ((d<a && d<b &&d<c &&d<e)&(d!=0))
min = d;
else if ((e<a && d<b &&d<c &&d<e)&(d!=0))
min = e;

System.out.println("your minimum is" +  min);
the user enters
0
3
5
1
3
" your minimum is ' 0' "

and i want to ignore the '0' and to output '1' for example

16. ## Re: Need Help in my First Java Program

i want to ignore the '0'
Then add the needed tests to ignore the 0s.

Did you see this?
What if one of the variables is 0? Then none of the other variables will be less than it.
What if b is 0? Will any of the if tests be true?

Is min ever assigned a value? Hint: Give it an initial value of -99 to see!!!

17. Member
Join Date
Nov 2014
Posts
10
Rep Power
0

## Re: Need Help in my First Java Program

Originally Posted by Norm
Then add the needed tests to ignore the 0s.

Did you see this?
What if one of the variables is 0? Then none of the other variables will be less than it.
What if b is 0? Will any of the if tests be true?

Is min ever assigned a value? Hint: Give it an initial value of -99 to see!!!
i changed it to

Java Code:
int min = -99;
and still not working .
i am sorry i dont understand exactly what to do .if you could be more precise i will appreciate it , what do i have to change in my code and where?/
every " if " says that if a variable is smallest than all the others .then its the smallest one.
but , what about if this value is '0' , it still the smallest one ,but not the minimum that i want to have .

18. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Need Help in my First Java Program

One observation which may not have been apparent. Once you decide that a certain value (a,b,c,d,or e) is not the maximum, you can exclude it from future comparisons. And then exclude the next one from future comparisons. Which means for five values, you only need four comparison statements (if none of a,b,c,or d is the maximum, it must be e).

Regards,
Jim

19. ## Re: Need Help in my First Java Program

Do you know how to make a flow chart to map logic? Or pseudo code?

I suggest that you stop making code changes and do some design work to find the logic needed to solve the problem. When you have good logic that solves the problem then write code to implement it.

20. Member
Join Date
Nov 2014
Posts
10
Rep Power
0

## Re: Need Help in my First Java Program

Originally Posted by Norm
Do you know how to make a flow chart to map logic.
this is how i created my code.
but i didnt know how to solve it with the '0'

Page 1 of 2 12 Last

#### Posting Permissions

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