Results 1 to 3 of 3
- 10-25-2011, 10:05 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 5
- Rep Power
- 0
Why is a negative percent not giving me the correct result?
When I input a negative number, it gives me an "E" when it should ask the user to enter a valid percentage, and I don't understand why :\
It works well with any positive integer :\....
Java Code:import java.util.*; public class Grade { public static void main (String[]args) { Scanner kybd = new Scanner(System.in); System.out.print("Please input a percentage: "); int PERCENT = kybd.nextInt(); //output grade based on percent if (PERCENT >= 70) { System.out.println("A"); } else if (PERCENT >= 60) { System.out.println("B"); } else if (PERCENT >= 50) { System.out.println("C"); } else if (PERCENT >= 40) { System.out.println("D"); } else if (PERCENT >= 0) { System.out.println("E"); } else if (PERCENT < 0) { System.out.println("Please enter a valid percentage"); } } }
- 10-25-2011, 10:25 PM #2
Member
- Join Date
- Sep 2010
- Posts
- 16
- Rep Power
- 0
Re: Why is a negative percent not giving me the correct result?
It works for me:
Oh, and don't capitalize your variable names. By convention, capitalized variable names are reserved for constants.
Java Code:$ cat Grade.java import java.util.*; public class Grade { public static void main (String[]args) { Scanner kybd = new Scanner(System.in); System.out.print("Please input a percentage: "); int PERCENT = kybd.nextInt(); //output grade based on percent if (PERCENT >= 70) { System.out.println("A"); } else if (PERCENT >= 60) { System.out.println("B"); } else if (PERCENT >= 50) { System.out.println("C"); } else if (PERCENT >= 40) { System.out.println("D"); } else if (PERCENT >= 0) { System.out.println("E"); } else if (PERCENT < 0) { System.out.println("Please enter a valid percentage"); } } } $ javac Grade.java $ java Grade Please input a percentage: -05 Please enter a valid percentage
- 10-25-2011, 10:26 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 5
- Rep Power
- 0
Re: Why is a negative percent not giving me the correct result?
Figured it out now, please do lock and/or delete this.
EDIT: Sorry, didn't realise anyone had replied. It seems it was just my NetBeans messing up. I restarted it and it worked fine :)
EDIT2: And thank you for the info, +rep ^_^Last edited by MXA92; 10-25-2011 at 10:29 PM.
Similar Threads
-
Struts 2 error : No result defined for action / result
By sameerk in forum Web FrameworksReplies: 1Last Post: 05-17-2011, 10:15 AM -
passing percent to public int method
By BillyB in forum New To JavaReplies: 3Last Post: 02-04-2011, 06:08 AM -
Help with negative numbers
By cela0811 in forum New To JavaReplies: 2Last Post: 01-04-2011, 05:04 PM -
In debug a javabean, how check it is giving to other component CORRECT data-DAO/pools
By lse123 in forum JDBCReplies: 22Last Post: 05-01-2010, 10:52 PM -
Negative elements
By swikar.java in forum New To JavaReplies: 6Last Post: 12-15-2008, 04:48 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks