Results 1 to 3 of 3
  1. #1
    MXA92 is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default 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");
                } 
        }
    }

  2. #2
    SFNYC is offline Member
    Join Date
    Sep 2010
    Posts
    16
    Rep Power
    0

    Default 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

  3. #3
    MXA92 is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default 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 11:29 PM.

Similar Threads

  1. Struts 2 error : No result defined for action / result
    By sameerk in forum Web Frameworks
    Replies: 1
    Last Post: 05-17-2011, 11:15 AM
  2. passing percent to public int method
    By BillyB in forum New To Java
    Replies: 3
    Last Post: 02-04-2011, 07:08 AM
  3. Help with negative numbers
    By cela0811 in forum New To Java
    Replies: 2
    Last Post: 01-04-2011, 06:04 PM
  4. Replies: 22
    Last Post: 05-01-2010, 11:52 PM
  5. Negative elements
    By swikar.java in forum New To Java
    Replies: 6
    Last Post: 12-15-2008, 05:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •