Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    elias2903 is offline Member
    Join Date
    Nov 2014
    Posts
    10
    Rep Power
    0

    Default 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

    Thanks in advance
    Last edited by elias2903; 11-21-2014 at 07:31 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    elias2903 is offline Member
    Join Date
    Nov 2014
    Posts
    10
    Rep Power
    0

    Default 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. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need Help in my First Java Program

    Where is the code that tests for the minimum value not equal to 0?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    elias2903 is offline Member
    Join Date
    Nov 2014
    Posts
    10
    Rep Power
    0

    Default Re: Need Help in my First Java Program

    Quote Originally Posted by Norm View Post
    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. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    elias2903 is offline Member
    Join Date
    Nov 2014
    Posts
    10
    Rep Power
    0

    Default Re: Need Help in my First Java Program

    Quote Originally Posted by Norm View Post
    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. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need Help in my First Java Program

    Where is the test for 0?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    elias2903 is offline Member
    Join Date
    Nov 2014
    Posts
    10
    Rep Power
    0

    Default Re: Need Help in my First Java Program

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

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    elias2903 is offline Member
    Join Date
    Nov 2014
    Posts
    10
    Rep Power
    0

    Default Re: Need Help in my First Java Program

    Quote Originally Posted by Norm View Post
    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. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need Help in my First Java Program

    minimum number that is not 0
    Use (number != 0) to make that test
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    elias2903 is offline Member
    Join Date
    Nov 2014
    Posts
    10
    Rep Power
    0

    Default Re: Need Help in my First Java Program

    Quote Originally Posted by Norm View Post
    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. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    elias2903 is offline Member
    Join Date
    Nov 2014
    Posts
    10
    Rep Power
    0

    Default Re: Need Help in my First Java Program

    Quote Originally Posted by Norm View Post
    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. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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!!!
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    elias2903 is offline Member
    Join Date
    Nov 2014
    Posts
    10
    Rep Power
    0

    Default Re: Need Help in my First Java Program

    Quote Originally Posted by Norm View Post
    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. #18
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    elias2903 is offline Member
    Join Date
    Nov 2014
    Posts
    10
    Rep Power
    0

    Default Re: Need Help in my First Java Program

    Quote Originally Posted by Norm View Post
    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 LastLast

Similar Threads

  1. Replies: 6
    Last Post: 12-15-2013, 07:40 AM
  2. Replies: 4
    Last Post: 11-21-2012, 02:19 PM
  3. Replies: 4
    Last Post: 07-30-2010, 01:25 PM
  4. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

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