Author |
Message |
slider203
|
Posted: Fri Mar 12, 2010 8:36 pm Post subject: Program wont loop |
|
|
Hello I need this program to loop ten times but I cant make it work...
Quote:
import java.io.*;
class SelectionPrac2
{
public static void main (String args [])
throws java.io.IOException
{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
int number = 0;
String input = "";
final int stop = 10;
System.out.println ("Enter "+stop+" numbers:");
for(int counter = 1; counter == stop; counter++)
System.out.print ("Please enter a number.");
input = br.readLine ();
number = Integer.parseInt (input);
if (number > 0)
{
System.out.println ("Positive Number");
}
else if (number < 0)
{
System.out.println ("Negative Number");
}
else if (number == 0)
{
System.out.println ("Zero");
}
}
}
I want this to ask the user to enter a number 10 times and then have the program tell if it is positive negative or zero.
The program only does this once though thankyou any help is appreciated. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wmano
|
Posted: Fri Mar 12, 2010 9:03 pm Post subject: RE:Program wont loop |
|
|
I believe you should look at your braces in the code, since the for loop has no braces. So basically the system.out is being executed 10 times but the rest of your code which gets the input and does the comparing of value isn't. |
|
|
|
|
|
TheGuardian001
|
Posted: Fri Mar 12, 2010 9:04 pm Post subject: Re: Program wont loop |
|
|
On top of that, The second condition of your for loop is wrong. The for loop structure is:
code: |
for (counter start; continue condition;increment condition){}
|
The second condition is the condition of when the loop should continue to execute, not when it should exit. Your loop says only to continue so long as counter == stop. |
|
|
|
|
|
X Abstract X
|
Posted: Thu Mar 18, 2010 9:06 pm Post subject: Re: Program wont loop |
|
|
I personally always learn better by seeing a full solution so I cleaned up your code a little.
|
|
|
|
|
|
TheGuardian001
|
Posted: Thu Mar 18, 2010 9:21 pm Post subject: Re: Program wont loop |
|
|
Abstract, we try to refrain from posting full solutions here. A large number of the people who come here for help are students, and providing full solutions tends to encourage people to simply use the full response without looking at it. |
|
|
|
|
|
X Abstract X
|
Posted: Thu Mar 18, 2010 9:25 pm Post subject: Re: Program wont loop |
|
|
Ah, sorry about that. I tried to edit it out but I can't now. |
|
|
|
|
|
TheGuardian001
|
Posted: Thu Mar 18, 2010 10:17 pm Post subject: Re: Program wont loop |
|
|
In this case it really doesn't matter, since they had the actual program working, but just got the for loops structure a bit wrong. Just wanted to be sure you wouldn't do it when somebody comes here looking for people to do their work for them (it does happen on occasion.) |
|
|
|
|
|
SNIPERDUDE
|
Posted: Thu Mar 18, 2010 10:46 pm Post subject: RE:Program wont loop |
|
|
Even when their program ends up working, and you still notice something you should let them know about - try hinting them towards the problem, and a possible solution. Perhaps sometimes even a link to a tutorial on the subject.
Anyhow I was a complete tool when I first joined these forums, we all learn this (among many) lesson at some point. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|