# Best way to do this code

• 03-01-2011, 03:36 AM
coding
Best way to do this code
Quote:

import jpb.SimpleIO;

public static void main(String[] args) {
SimpleIO.prompt("Enter a number from 1-100");
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.
• 03-01-2011, 03:42 AM
sunde887
You are looking for
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.
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
Code:

```if(count == 3){   System.exit(0); }```
• 03-01-2011, 04:22 AM
coding
Quote:

import jpb.SimpleIO;

public static void main(String[] args) {
SimpleIO.prompt("Enter a number from 1-100");
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 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.
• 03-01-2011, 04:31 AM
sunde887
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

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.
• 03-01-2011, 04:41 AM
coding
Alright. I will try that after some sleep.
• 03-01-2011, 04:44 AM
Junky
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.
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.
• 03-01-2011, 04:56 AM
sunde887
If you want to test if some number is in range you need to use &&

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