Results 1 to 13 of 13
  1. #1
    jjfstar2009 is offline Member
    Join Date
    Apr 2014
    Posts
    21
    Rep Power
    0

    Exclamation I have a test on this today, Please Help with the Java problem

    Write a java program that will ask a user to input grades until the user inputs the sentinel value -1. The program should test each input to determine whether 0<=grade<=100. If the grade entered is within this range, the program should print "Grade accepted;" if not, the program should print "Invalid input".

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: I have a test on this today, Please Help with the Java problem

    Did you try anything so far? If you do not have a clue how to start and you have a test today, then I have no idea how we could help you.

    Konrad

  3. #3
    jjfstar2009 is offline Member
    Join Date
    Apr 2014
    Posts
    21
    Rep Power
    0

    Default Re: I have a test on this today, Please Help with the Java problem

    public class gradeTester
    {
    public static void main (String [] args){
    Scanner in = new Scanner(System.in);
    int number = in.nextInt();
    System.out.println("Enter a grade or -1 to quit: ");
    while (number!=-1){
    if ((0<=number) && (number<=100))
    {
    System.out.println("Grade accepted");
    }
    else
    {
    System.out.println("Grade not accepted");
    }
    number = in.nextInt();
    System.out.println("Enter a grade: ");
    }
    }
    }

  4. #4
    jjfstar2009 is offline Member
    Join Date
    Apr 2014
    Posts
    21
    Rep Power
    0

    Default Re: I have a test on this today, Please Help with the Java problem

    It doesn't print out anything, but the programs runs

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: I have a test on this today, Please Help with the Java problem

    Java Code:
    if ((0<=number) && (number<=100))
    pretty sure you want that to be:

    Java Code:
    if ((number >= 0) && (number<=100))
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    jjfstar2009 is offline Member
    Join Date
    Apr 2014
    Posts
    21
    Rep Power
    0

    Default Re: I have a test on this today, Please Help with the Java problem

    oh thanks. i didn't think about it.

  7. #7
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: I have a test on this today, Please Help with the Java problem

    Hi,

    that was a great first approach. If you check your code, then you first read the value that the user entered and afterwards you ask the user to enter a number. You should simply switch these commands (They are in your code at 2 locations.)

    With kind regards,

    Konrad

  8. #8
    jjfstar2009 is offline Member
    Join Date
    Apr 2014
    Posts
    21
    Rep Power
    0

    Default Re: I have a test on this today, Please Help with the Java problem

    It doesn't doesn't print out anything :(

  9. #9
    jjfstar2009 is offline Member
    Join Date
    Apr 2014
    Posts
    21
    Rep Power
    0

    Default Re: I have a test on this today, Please Help with the Java problem

    thank for that last comment, it got fixed. Thanks a lot for helping me. Sorry at first for not posting the worked out program.

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

    Default Re: I have a test on this today, Please Help with the Java problem

    Quote Originally Posted by gimbal2 View Post
    Java Code:
    if ((0<=number) && (number<=100))
    pretty sure you want that to be:

    Java Code:
    if ((number >= 0) && (number<=100))
    Color me stupid, but what is wrong with the original version?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    jjfstar2009 is offline Member
    Join Date
    Apr 2014
    Posts
    21
    Rep Power
    0

    Default Re: I have a test on this today, Please Help with the Java problem

    if ((0<=number) && (number<=100)) <---- this is the correct way to do the problem

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: I have a test on this today, Please Help with the Java problem

    Quote Originally Posted by JosAH View Post
    Color me stupid, but what is wrong with the original version?

    kind regards,

    Jos
    No color me stupid for assuming too much and color the OP stupid for blindly listening to me without thinking about it AGAIN.

    I'm going to stand in the corner of shame for a while, just leave my mail at the front desk.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: I have a test on this today, Please Help with the Java problem

    Quote Originally Posted by jjfstar2009 View Post
    if ((0<=number) && (number<=100)) <---- this is the correct way to do the problem
    And what is 'correct' about it compared to the first version?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 12-31-2013, 06:10 AM
  2. Replies: 20
    Last Post: 05-09-2013, 07:44 PM
  3. Replies: 11
    Last Post: 11-11-2011, 07:36 AM
  4. Replies: 0
    Last Post: 04-05-2008, 12:38 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
  •