Results 1 to 7 of 7
  1. #1
    coding is offline Member
    Join Date
    Jan 2011
    Posts
    40
    Rep Power
    0

    Default Best way to do this code

    import jpb.SimpleIO;
    public class GradeReport {


    public static void main(String[] args) {
    SimpleIO.prompt("Enter a number from 1-100");
    String grade = SimpleIO.readLine();
    int number = Integer.parseInt(grade);
    int i = 0;
    if ( number > 100)
    i+=1;
    {
    System.out.println("You must enter an integer between 0-100. You have tried "+i);
    SimpleIO.prompt("Enter a number 0-100");
    }
    if ( number < 0)
    i+=1;
    {
    System.out.println("You must enter an integer between 0-100");
    SimpleIO.prompt("Enter a number 0-100");
    }
    if (i >= 3)
    {
    System.out.println("You exceeded the amount of tries");
    }
    }
    }
    Is there an easy way to make it so that when its greater than 100 or less than or equal to zero in one line? Like if number >100 && number < 0 do.....
    Also how do I get it so when enter a number and fail, I can enter a number again. After I do it once I have to run it again. I want it to stop after the 3 try.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are looking for
    Java Code:
    if(number < 0 || number > 100){
      prompt for new numbers
    You can also have a loop which gives you 3 shots to get it correct.
    Java Code:
    int count = 0;
    while(count < 3){
      if(number < 0 || number > 100){
        count++;
      }
    }
    if its a correct input you can break the loop, if it's incorrect and count reaches 3 you can have a test outside of the loop which tests if count == 3, if it is you can end the program
    Java Code:
    if(count == 3){
      System.exit(0);
    }

  3. #3
    coding is offline Member
    Join Date
    Jan 2011
    Posts
    40
    Rep Power
    0

    Default

    import jpb.SimpleIO;
    public class GradeReport {


    public static void main(String[] args) {
    SimpleIO.prompt("Enter a number from 1-100");
    String grade = SimpleIO.readLine();
    int number = Integer.parseInt(grade);
    if(number < 0 || number > 100){
    {
    SimpleIO.prompt("Enter a number from 1-100");
    String grades = SimpleIO.readLine();
    if(number < 0 || number > 100){
    SimpleIO.prompt("Enter a number from 1-100");

    }
    }
    int count = 0;
    while(count < 3){
    if(number < 0 || number > 100){
    count++;
    }
    String gradess = SimpleIO.readLine();
    int numbers = Integer.parseInt(gradess);
    int category = 0;
    if (number > 0 || number < 101 )
    category = number/10;
    System.out.println("Your grade is"+ category);

    if(count == 3){
    System.exit(0);
    }
    }
    }
    }
    }
    Alright. I did that now. But how do I make it so when I enter a number less than 101 and greater than zero it divides it by 10 and gives that answer back? I tried it but I won't receive an answer back.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Java Code:
    int number = Integer.parseInt(grade);
    if(number < 0 || number > 100){
    {
    SimpleIO.prompt("Enter a number from 1-100");
    String grades = SimpleIO.readLine();
    if(number < 0 || number > 100){
    SimpleIO.prompt("Enter a number from 1-100");
    
    }
    }
    Lots of repetition here.

    Try prompting for the value inside of of the while loop

    Java Code:
    while(condition){
      prompt for value
      if(condition){
        statements;
      }
      else if(condition){
        break;
      }
    }
    if(condition){
      statements
    }
    else{
      statements
    }
    This is how the final code should look, unfortunately I left the important parts up to you. Good luck.
    Last edited by sunde887; 03-01-2011 at 03:58 AM.

  5. #5
    coding is offline Member
    Join Date
    Jan 2011
    Posts
    40
    Rep Power
    0

    Default

    Alright. I will try that after some sleep.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    There are so many things wrong with your code it is hard to know where to start.

    Why do you display the prompt in several places instead of one?
    Why do you read input in three place, into three different variables.
    Java Code:
    if (number > 0 || number < 101 )
    When will that ever be false? 5000 is greater htan 0 therefore true and -42 is less than 101 therefore true.
    The last if statement is pointless if it is false then the program will exit anyway.

    What you need to do is step away from the computer, grab a pencil and paper and write down the simple steps you need to take. Once you have done this, expand those simple steps into more detail steps/instructions. Finally convert that into code.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you want to test if some number is in range you need to use &&

    Java Code:
    /**
     * you use && here because you want it to only produces true if
     * BOTH conditions are true
     * if the number is over 101, you know immediately it's a bad number
     * and this statement will produce false
     * same for -1
     **/
    if(number < 101 && number > 0)
    However, if you want to test if a number is outside you don't want &&. If you do use && it will always be false
    Java Code:
    /**
     * always produces false
     * if the number is 101, it is greater than 101, but it fails the 
     * number < 0 portion
     */
    if(number < 0 && number > 100)
    because of this, you use
    Java Code:
    if(number < 0 || number > 100)
    One final thing, in the loop you are testing if the number is good or bad, if it's bad you prompt and increase count.

    If it's good though you can break out of this loop, therefore you don't need to test if the number is good or not.

    Before doing any calculations you should test if the the person took more than 3 tries(count), and exit if they did. else do tests.

    I gave you a lot here, and some of it was just explaining some logic to you. But junky is right. Take some time, jot down your thoughts on paper and work out what and HOW you want to solve this. My pseudocode is nearly the perfect solution. It shows you when to do what, you just need to fill in conditions and statements.

Similar Threads

  1. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  2. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  3. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

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