Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default any suggestions?

    how do i get this program's output to display both largerst and the smallest digit.

    i alwasys get either one of them.

    Java Code:
    import java.util.Scanner;
    
    	public class hw1 {
    
    	public static void main (String[]args){
    
    	Scanner input =  new Scanner(System.in);
    
    	System.out.println("Enter 4-digit of integer: ");
    
    		int a = input.nextInt();
    
    		int b = a % 10;
    
    		int x = a/10;
    
    		int c = x % 10;
    
    		int y = x / 10;
    
    		int d = y % 10;
    
    		int z = y / 10;
    
    		int e = z % 10;
    
    
    		if (a>0){
    
    		System.out.println(b);
    		System.out.println(c);
    		System.out.println(d);
    		System.out.println(e);
    }
    
    		if ((b>= c)&& ( c >= d)&&(d >= e)){
    		System.out.println("Largest digit value is "+ b);
    }
    		else if ((c>= d)&&(d >= e)&&(e>= b)){
    		System.out.println("Largest digit value is "+ c);
    }
    		else if ((d>= e)&& ( e>= b)&&(b >= c)){
    		System.out.println("Largest digit value is "+ d);
    }
    		else if ((e>= b)&&(b >= c)&&(c >= d)){
    		System.out.println("Largest digit value is "+ e);
    }
    
    		if ((b <= c)&&(c <= d)&&(d <= e)){
    		System.out.println("Smallest digit value is "+ b);
    }
    		else if ((c <= d)&&(d <= e)&&(e <= b)){
    		System.out.println("Smallest digit value is "+ c);
    }
    		else if ((d <=e )&&(e <= b)&&( b<= c)){
    		System.out.println("Smallest digit value is "+ d);
    }
    		else if ((e <= b)&&(b <= c)&&(c <= d)){
    		System.out.println("Smallest digit value is "+ e);
    }
    
    
    }
    
    }

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Observations and suggestions

    Comment:
    • The comparation of a>0 should be done at the very begining of the program (after the value of "a" is obtained).

    Suggestions:
    Your comparation for the largest/smallest is not correct.
    • If I input 1234, it will only display the largest number
    • If I input 4321 it will only display the lowest number and ...
    • If I input 1539, neither is displayed
    Try a different approach:
    Compare the first digit against the rest of the digits. For example:
    a=1537
    • if b>c, then largest = b
    • if not, then c is larger than b therefore largest = c
    • then comparare "largest" against the next digit. Once you find a larger number, make it "largest" value
    • Do the same for the "smallest" value


    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    what you are suggesting a little confusing to me.
    if i put if(a>0) at the very beginning, it wouldnt work for the rest of the calculations. i thought you can't put e.g ( int b = a % 10; ) inside the if loop.

    what about this
    Java Code:
    		if (b> c){
    			if (b>d)
    				if (b>e)
    		System.out.println("Largest digit value is "+ b);
    }
    Last edited by PureAwesomeness; 01-19-2009 at 01:47 AM.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default samples...

    What is the purpose of checking if a>0? To make sure that the user has entered a valid digit--- right? i was thinking of something on the terms of:
    Java Code:
    int a = input.nextInt();
    if (a>0)
    {
      int b = a % 10;
      int x = a/10;
      int c = x % 10;
      int y = x / 10;
      int d = y % 10;
      int z = y / 10;
      int e = z % 10;
              .
              .
              .
     }
    else 
     {
       System.out.println("Enter a valid 4-digit of integer (ex: 1234)");
       return;
      }
    Anyway... for the comparation, you have to use another variable, which I will call "largest" (some pseudo code):
    Java Code:
    if b> c then 
    largest = b;
    else 
    largest = c;
    if largest < d then
    largest = d;
    else 
    if largest < e
    largest = e;
    In the end the largest value is in the "largest" variable.
    Just do the same for the smallest value.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Why don't you just use Math.min() or Math.max() to get the effect you want? I think it is more comfortable :)

Similar Threads

  1. Suggestions for my Java-project?
    By shabbee in forum New To Java
    Replies: 1
    Last Post: 01-01-2009, 11:11 PM
  2. Beginning Game Development - Suggestions?
    By JDCAce in forum Advanced Java
    Replies: 1
    Last Post: 12-08-2008, 06:42 PM
  3. Suggestions of crufty or otherwise bad APIs
    By Andre in forum Forum Lobby
    Replies: 8
    Last Post: 11-18-2008, 12:06 AM
  4. Any Datastructure Suggestions For An Unbalanced Tree?
    By markus-sukram in forum New To Java
    Replies: 0
    Last Post: 03-26-2008, 03:25 PM
  5. Java application help and suggestions required
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-18-2007, 04:11 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
  •