# Thread: Best way to do this code

1. Member
Join Date
Jan 2011
Posts
40
Rep Power
0

## 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");
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. 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. Member
Join Date
Jan 2011
Posts
40
Rep Power
0
import jpb.SimpleIO;
public class GradeReport {

public static void main(String[] args) {
SimpleIO.prompt("Enter a number from 1-100");
int number = Integer.parseInt(grade);
if(number < 0 || number > 100){
{
SimpleIO.prompt("Enter a number from 1-100");
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++;
}
int numbers = Integer.parseInt(gradess);
int category = 0;
if (number > 0 || number < 101 )
category = number/10;

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. Java Code:
```int number = Integer.parseInt(grade);
if(number < 0 || number > 100){
{
SimpleIO.prompt("Enter a number from 1-100");
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 04:58 AM.

5. Member
Join Date
Jan 2011
Posts
40
Rep Power
0
Alright. I will try that after some sleep.

6. 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. 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.

#### Posting Permissions

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