Results 1 to 5 of 5
  1. #1
    hydride is offline Member
    Join Date
    Jan 2010
    Posts
    15
    Rep Power
    0

    Default reduce redundancy

    I want to make this code shorter but still perform the same way.

    Java Code:
    		Scanner input = new Scanner(System.in);
    		System.out.print("Enter first number: ");
    		if (input.nextInt() > var3) {
    			System.out.print("Enter second number: ");
    			int num2 = input.nextInt();
    			System.out.println("First is greater");
    			if (num2 < var3)
    				System.out.println("Second is Less than");
    			else
    				System.out.println("Second is Greater or equal");
    		} else {
    			System.out.print("Enter second number: ");
    			int num2 = input.nextInt();
    			System.out.println("First is Less than or equal");
    			if (num2 < var3)
    				System.out.println("Second is Less than");
    			else
    				System.out.println("Second is Greater or equal");
    		}

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    hydride is offline Member
    Join Date
    Jan 2010
    Posts
    15
    Rep Power
    0

    Default

    sorry, var3 = 5

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,450
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by hydride View Post
    I want to make this code shorter but still perform the same way.
    Just rearrange your code a bit:

    Java Code:
    		
    Scanner input = new Scanner(System.in);
    System.out.print("Enter first number: ");
    int num1= input.nextInt();
    System.out.print("Enter second number: ");
    int num2 = input.nextInt();
    
    if (num1 > var3) 
    	System.out.println("First is greater");
    else 
    	System.out.println("First is Less than or equal");
    
    if (num2 < var3)
    	System.out.println("Second is Less than");
    else
    	System.out.println("Second is Greater or equal");
    kind regards,

    Jos

Similar Threads

  1. Replies: 2
    Last Post: 12-30-2008, 02:10 PM
  2. How do I reduce Fractions in this program?
    By Popedreadlock in forum New To Java
    Replies: 8
    Last Post: 12-08-2008, 12:28 AM
  3. Replies: 0
    Last Post: 04-07-2008, 07:56 PM
  4. How to reduce the size or avoiding out of memory error?
    By rajeshkumarmsc in forum Advanced Java
    Replies: 3
    Last Post: 08-11-2007, 10:15 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
  •