Results 1 to 5 of 5
Thread: reduce redundancy
- 04-06-2010, 06:23 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 15
- Rep Power
- 0
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"); }
- 04-06-2010, 06:27 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What you exactly want to do? For me your code is harder to read.
- 04-06-2010, 06:27 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
And also what's var3? Where you define it?
- 04-06-2010, 06:50 PM #4
Member
- Join Date
- Jan 2010
- Posts
- 15
- Rep Power
- 0
sorry, var3 = 5
- 04-06-2010, 08:25 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
Just rearrange your code a bit:
kind regards,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");
Jos
Similar Threads
-
How to eliminate 'reduce to icon' and 'maximize' buttons.
By hendrix79 in forum New To JavaReplies: 2Last Post: 12-30-2008, 02:10 PM -
How do I reduce Fractions in this program?
By Popedreadlock in forum New To JavaReplies: 8Last Post: 12-08-2008, 12:28 AM -
How to use Java's compression classes to reduce the amount of data sent over a socket
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 07:56 PM -
How to reduce the size or avoiding out of memory error?
By rajeshkumarmsc in forum Advanced JavaReplies: 3Last Post: 08-11-2007, 10:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks