This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Background: This was homework. It was due last week but as it is past due I am just asking becuase my failure to get this is bugging me. The prof doesn't include much commenting in his grades, and I haven't put any comments in yet as I usually do that at the end(probably a bad practice).
Basically I don't understand why my efforts to automatically play again are failing. When I run this it just automatically completes. The initial while loop should repeat, but I'm never even prompted to re-evalutate the string "play_again".
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
/** * * @author ldl2 */ import java.util.Scanner; import java.util.Random;
public class derp_dada {
public static void main(String[] arg) {
int guess, actual, guess_number;
String play_again = "y";
Scanner scan = new Scanner(System.in);
Random rand = new Random();
actual = rand.nextInt(100);
guess_number = 0;
while (play_again.equalsIgnoreCase("y")) {
while (guess_number <= 5) {
System.out.println("What number do you guess?");
guess = scan.nextInt();
if (guess > 100) {
System.out.println("That isn't valid");
} else if (guess < 1) {
System.out.println("That isn't valid");
} else if (guess < actual) {
System.out.println("That's too low.");
} else if (guess > actual) {
System.out.println("That's too high.");
} else {
System.out.println("you got it! and you only made " guess_number " guesses");
guess_number = 5;
}
guess_number = 1;
if (guess_number > 5) {
System.out.println("You are done guessing");
}
}
play_again = (String) scan.nextLine();
System.out.println("Would you like to play again? ((Y)es/(N)o)");
}
}
}
Subreddit
Post Details
- Posted
- 8 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnjava/c...