Results 1 to 7 of 7
Thread: Best way to do this code
- 03-01-2011, 02:36 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 40
- Rep Power
- 0
Best way to do this code
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.....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");
}
}
}
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, 02:42 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You are looking for
You can also have a loop which gives you 3 shots to get it correct.Java Code:if(number < 0 || number > 100){ prompt for new numbers
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 programJava Code:int count = 0; while(count < 3){ if(number < 0 || number > 100){ count++; } }
Java Code:if(count == 3){ System.exit(0); }
- 03-01-2011, 03:22 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 40
- Rep Power
- 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.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);
}
}
}
}
}
- 03-01-2011, 03:31 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Lots of repetition here.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"); } }
Try prompting for the value inside of of the while loop
This is how the final code should look, unfortunately I left the important parts up to you. Good luck.Java Code:while(condition){ prompt for value if(condition){ statements; } else if(condition){ break; } } if(condition){ statements } else{ statements }Last edited by sunde887; 03-01-2011 at 03:58 AM.
- 03-01-2011, 03:41 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 40
- Rep Power
- 0
Alright. I will try that after some sleep.
- 03-01-2011, 03:44 AM #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.
When will that ever be false? 5000 is greater htan 0 therefore true and -42 is less than 101 therefore true.Java Code:if (number > 0 || number < 101 )
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, 03:56 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
If you want to test if some number is in range you need to use &&
However, if you want to test if a number is outside you don't want &&. If you do use && it will always be falseJava 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)
because of this, you useJava 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)
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.Java Code:if(number < 0 || number > 100)
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
-
C server code - Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02-21-2011, 11:50 AM -
can any one pls send me a sample code for calling a jsp code in swings
By sniffer139 in forum AWT / SwingReplies: 1Last Post: 03-04-2010, 11:19 AM -
Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08-12-2009, 11:14 AM -
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 01:21 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks