# Why is a negative percent not giving me the correct result?

• 10-25-2011, 11:05 PM
MXA92
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 :\....

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, 11:25 PM
SFNYC
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.

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, 11:26 PM
MXA92
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 ^_^