Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Program wont loop
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
slider203




PostPosted: 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
Sponsor
sponsor
wmano




PostPosted: 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




PostPosted: 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




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

Java:

import java.io.*;

public class SelectionPrac2 {

        public static void main(String[] args) throws IOException {

                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                int number = 0;

                System.out.println("Enter 10 numbers.");

                for (int i = 0; i < 10; i++) {
                        System.out.println("Please enter a number.");

                        number = Integer.parseInt(br.readLine());

                        if (number > 0)
                                System.out.println("Positive number.");
                        else if (number < 0)
                                System.out.println("Negative number.");
                        else
                                System.out.println("Zero.");
                }

        }

}
TheGuardian001




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: