2 Attachment(s)
do while loop with nested while question
hey guys here is a program i'm working on. I cant get it to count valid guesses 1-20 it only counts invalid and for some reason on my net beans ide it never say build successful even when the random number is guessed and the you won is displayed. any thoughts are very appreciated.:oAttachment 218
Attachment 219
Re: do while loop with nested while question
It would be alot easier to help you if you posted all of your code, and surround it in [highlight=java][/highlight]. I have no clue on the netbeans problem. I strongly dislike NB myself.
Re: do while loop with nested while question
Please copy and paste the code into a post directly (remember to use the code tags). As is it is difficult to give much guidance
Re: do while loop with nested while question
Quote:
Originally Posted by
copeg
Please copy and paste the code into a post directly (remember to use the code tags). As is it is difficult to give much guidance
Gotcha Copeg ;)
Re: do while loop with nested while question
hey guys thanks for the info I cant figure out to convert the source file into someting i can attach so i'll just write it out. the program prompt users for a number between 1 and 20 then generates a random number. only ints entered in the range of 1 to 20 are supposed to count as guesses here goes.
Code :
import java.util.Scanner;
import java.util.Random
public class labAssign5 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int rNumber, userGuess, totalGuesses;
Random rGenerate = new Random( );
rNumber = rGenerate.nextInt(20) + 1;
totalGuesses = 0
do
{System.out.print("Enter a number 1 to 20 -> ");
userGuess = keyboard.nextInt( );
{
while (userGuess<= 20 && userGuess >=1 && userGuess != rNumber)
{ System.out.print("Try again.\nEnter a number 1 to 20 -> ";
userGuess = keyboard.nextInt( );
}
totalGuesses ++;
if (userGuess == rNumber)
{
System.out.print("You won!\nThe number of guesses is " + totalGuesses);
userGuess = keyboard.nextInt( );
}
}
} while (userGuess <1 || userGuess >20);
System.exit(0);
}
}
Re: do while loop with nested while question
Just this once ;)
johneusmc, check the logic carefully. (if I understand the goal of the code...) Are you truly incrementing totalGuesses for each attempt? Place some println's to debug the code and you can see if userGuess gets incremented and where.
Re: do while loop with nested while question
okay thanks for the help copeg. if anyone is familiar with netBeans can u tell me why it keeps running until i press enter then on the blank line put in any number and press enter then it stops running and says build successful. There are no syntax errors.
Code Java:
/*This program prompts the user for a number 1-20 and compares it with a
* randomly generated number. If it matches the program displays you won.
* if not, it displays try again. The program also displays the number of
* guesses. It also does not count invalid entries as guesses no matter where in
* the program they are entered.
*/
import java.util.Scanner;
import java.util.Random;
/**
*
* @author John LaFave
*/
public class labAssign5 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int rNumber, userGuess;
int totalGuesses;
Random rGenerate = new Random();
rNumber = rGenerate.nextInt(20) + 1;
totalGuesses = 0;
do
{
System.out.print("Enter a number 1 to 20 -> ");
userGuess = keyboard.nextInt();
{
while (userGuess <=20 && userGuess >=1 && userGuess != rNumber)
{
System.out.print("Try again.\nEnter a number"
+ " 1 to 20 -> "); userGuess = keyboard.nextInt();
totalGuesses ++;
}
if (userGuess == rNumber)
{
totalGuesses ++;
System.out.print("You won!\nThe number of guesses is "
+ totalGuesses);
userGuess = keyboard.nextInt();
}
}
} while (userGuess < 1 || userGuess > 20);
System.exit(0);
} }
Re: do while loop with nested while question
Likely it's a problem with your timing and what order you're doing stuff in. Save your work, then close netbeans. This will effectively terminate any programs NetBeans could be running/building. Then re-start netbeans. Click the build button once. Be patient, it may take a while for it to finish building. Once it tells you that it's finished building, click the Run button once. Again, it may take a while for the program to start up. Also, once you're done, make sure your program has terminated (or forcibly terminate it). This will prevent any weird things from happening such as accidentally inputting data to the old program.
Re: do while loop w/ nested while. how to forces loop stop w/o break or sentinal valu
okay I figured out that my outer do-while loop wants to keep running when I want the program to end after my nested while loop gets the random number match. I can terimnate the program by entering an int into the console that makes the do-while false but that doesn't really work for an efficient program. I tried using break but it either stops the whole thing or does notjing depending on placement. Any thoughts would be awesome.oh and I tried nesting the do-while inside the while but couldn't get it to work. does anyone think thats the way to go or the force stop?
Re: do while loop with nested while question
You had a put System.in during your if loop, not sure why. Try this
Code java:
/*This program prompts the user for a number 1-20 and compares it with a
* randomly generated number. If it matches the program displays you won.
* if not, it displays try again. The program also displays the number of
* guesses. It also does not count invalid entries as guesses no matter where in
* the program they are entered.
*/
import java.util.Scanner;
import java.util.Random;
/**
*
* @author John LaFave
*/
public class labAssign5 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int rNumber, userGuess;
int totalGuesses;
Random rGenerate = new Random();
rNumber = rGenerate.nextInt(20) + 1;
totalGuesses = 0;
do
{
System.out.print("Enter a number 1 to 20 -> ");
userGuess = keyboard.nextInt();
{
while (userGuess <=20 && userGuess >=1 && userGuess != rNumber)
{
System.out.print("Try again.\n" +
"Enter a number 1 to 20 -> "); userGuess = keyboard.nextInt();
totalGuesses ++;
}
if (userGuess == rNumber)
{
totalGuesses ++;
System.out.print("You won!\nThe number of guesses is " + totalGuesses);
System.out.println("["+userGuess+"]" + "["+rNumber+"]");
}
}
} while (userGuess < 1 || userGuess > 20);
System.exit(0);
} }
The point of
Code java:
System.out.println("["+userGuess+"]" + "["+rNumber+"]");
is to test if those are even, if they are and it still doesn't end the while statement than I have no clue.
It will look funny, but if it ends like it should, just delete that line. I added my own style, you can change it if you want, shouldn't make a difference.
Also for testing purposes you could add rNumber = 1 and see if it ends like it should without guessing the number.
Re: do while loop with nested while question
Thanks a lot. That did it. appreciate the help.