
-----------------------------------
slider203
Fri Mar 12, 2010 8:36 pm

Program wont loop
-----------------------------------
Hello I need this program to loop ten times but I cant make it work...


import java.io.*;
class SelectionPrac2
{
  public static void main (String args 

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.

-----------------------------------
wmano
Fri Mar 12, 2010 9:03 pm

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
Fri Mar 12, 2010 9:04 pm

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){}
[/code]

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
Thu Mar 18, 2010 9:06 pm

Re: Program wont loop
-----------------------------------
I personally always learn better by seeing a full solution so I cleaned up your code a little.


import java.io.*;

public class SelectionPrac2 {

	public static void main(String

-----------------------------------
TheGuardian001
Thu Mar 18, 2010 9:21 pm

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
Thu Mar 18, 2010 9:25 pm

Re: Program wont loop
-----------------------------------
Ah, sorry about that.  I tried to edit it out but I can't now.

-----------------------------------
TheGuardian001
Thu Mar 18, 2010 10:17 pm

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
Thu Mar 18, 2010 10:46 pm

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.
