0

I FIGURED EVERYTHING OUT TY FOR THE HELP

When I type in letters at attempt 1/4 it works fine and it continues, but once I type a letter at attempt 2/4 it just prints the message and the program stops. Also any tips on #2, I can only think of if(guess>=4 && guess<=16) else statement(not sure if this is correct)

When I execute the code -

Guess a number between 1 and 16.

Attempt 1 of 4: 8

You guessed 8

Too Low! 

Attempt 2 of 4: a

Please enter an integer between 4-16     

can't enter anything after

Problems: I have to make exception handlers if user types in a

1) non-numeric input

2) out of range input

3) Have to retain current guess amount

import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;

public class GuessingGame {

static final int limit = 4;
static final int maxInteger = 16;

public static void main(String[] args) {

Random rand = new Random();
int target = rand.nextInt(maxInteger) + 1;
int x = 1;

    do{
    try{
        Scanner input = new Scanner(System.in);
        System.out.printf("Guess a number between 1 and %d.\n", maxInteger);


    int attempts = 1;
    while (attempts <= limit) {
        System.out.printf("Attempt %d of %d: ", attempts, limit);


        int guess = input.nextInt();
        System.out.printf("You guessed %d\n", guess);

        if(guess > target) {
        System.out.printf("Too High! \n");

        }
        else if(guess == target){

        System.out.printf("The answer is %d, You win!", guess);
            attempts = 20;

        }
        else{
        System.out.printf("Too Low! \n");

        }

        attempts+=1;
        x = 2;
    }
    if(attempts==5){
    System.out.println("You lose!");
    }
    }
    catch(InputMismatchException e){
        System.out.printf("Please enter an integer between 4-16");
        continue;


    }
    }while(x == 1);
}
}
3
  • Could you include the error trace? Commented Feb 16, 2016 at 21:10
  • what is your problem? You want to know how to write a new exception or that your exception is not caught? Commented Feb 16, 2016 at 21:10
  • added what happens when I execute the program at the top Commented Feb 16, 2016 at 21:13

2 Answers 2

2

You are checking while(x == 1); in the outer loop and from the inner loop you incremented the value of x by doing x = 2;. This will break the condition and you'll come out of the outer while loop.

You should be setting a valid condition for the outer while loop if you want to continue. Try something like while(x < 5);

Your code should look something like this:

do {
    try {
        ...
        /* Inner While */
        while() {}
        ...
    } catch () {
        /* Exception Handling */
        ...
    }
} while(x < 5); /* Decide a valid condition */
Sign up to request clarification or add additional context in comments.

8 Comments

I changed x = 2; into x++; and while(x < 5); My attempts reset to 1/4 if I type a letter in 2/4 but I want it to stay as 2/4, 3/4 , 4/4 their current attempts. and it doesn't execute the "You lose" in this scenario. Results now when I execute it. pastebin.com/5W1NwDuU
It is because you are initializing your attempts inside the do-while. You should declare it outside the do-while loop.
You have only one do-while loop. The other while is inside this do-while loop.
Couple of changes. Please change the class Name. Here you go: pastebin.com/KZWfvNbr
ty for help, I solved the problem. 2nd answer helped more but I guessing x and the boolean flag could be considered the same thing.
|
1

1) try moving your try catch block inside the inner loop so it only encloses

int guess = input.nextInt();

2) your idea for number 2 should work.

if(guess>=4 && guess<=16)

make sure this is the first if statement in your checks, then you don't have to change any of your other if statements.

3) make a variable outside both of the loops called guess, then instead of saying

int guess = input.nextInt();

just say

guess = input.nextInt();

The current guess wil be availble to you until you update it.

4) your variable x is confusing. Are you using it as a flag to end the outer loop? if that is the case make it a boolean

boolean flag = true;

then you can set the flag to false when you are ready to break out of the loop. change

x = 2;

to

flag = false;

also for the loop all change

while(x==1)

to

while(flag)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.