简体   繁体   中英

Java Exception handling looping error

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);
}
}

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 */

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM