Results 1 to 3 of 3
- 11-06-2011, 08:37 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
Problem with while loop, assigning a variable with a different value every loop? Help
Hey, this is a simple tester program I tried to put together. Only problem is that once a value is given for "counter", this value doesn't seem to change itself afterwards, despite the while loop.
Here it is anyway:
import java.util.Scanner;
class tut {
public static void main(String args []){
Scanner scan = new Scanner(System.in);
System.out.println("Would you like me to ask this question again? ");
String counter = scan.nextLine();
while(counter.equals("yes")){
main(args);
}
}
}
- 11-06-2011, 10:52 PM #2
Re: Problem with while loop, assigning a variable with a different value every loop?
I highly recommend NOT calling the main method from within your program. If you want the code repeating while user enters "yes" then use a do while loop instead and place the line asking the question and the line getting user input inside the loop. Also I would use equalsIgnoreCase to handle "YES", "Yes" etc.
- 11-07-2011, 02:25 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 19
- Rep Power
- 0
Re: Problem with while loop, assigning a variable with a different value every loop?
have in mind that in this code you will need to give as many "yes" as the count of any other reply in order to terminate. Keep giving yes and it will terminate.
The reason is that every time you type something else a new main is called and so one more yes is needed in order to end.
Similar Threads
-
multiply variable in for loop
By aconti in forum New To JavaReplies: 7Last Post: 06-03-2011, 12:24 PM -
Accessing a variable from a loop
By mhz041986 in forum New To JavaReplies: 4Last Post: 04-04-2011, 08:49 AM -
Variable not initialized after while loop
By ejs7597 in forum New To JavaReplies: 6Last Post: 02-28-2009, 05:00 AM -
Rediculous problem?! Assigning a fraction to a double variable
By Tzoshirzup in forum New To JavaReplies: 3Last Post: 11-24-2008, 07:01 PM -
[SOLVED] Referencing Variable within Loop
By Judoon_Platoon in forum Java AppletsReplies: 2Last Post: 05-22-2008, 11:54 AM
Bookmarks