简体   繁体   English

Java异常处理循环错误

[英]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. 当我尝试1/4键入字母时,它可以正常工作并继续,但是一旦我尝试2/4键入字母,它只会打印该消息,并且程序停止。 Also any tips on #2, I can only think of if(guess>=4 && guess<=16) else statement(not sure if this is correct) 还有关于#2的任何提示,我只能想到if(guess> = 4 &&猜测<= 16)else语句(不确定这是否正确)

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 1)非数字输入

2) out of range input 2)超出范围输入

3) Have to retain current guess amount 3)必须保留当前的猜测量

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); 您正在检查while(x == 1); in the outer loop and from the inner loop you incremented the value of x by doing x = 2; 在外循环和内循环中,通过使x = 2;增加x的值x = 2; . This will break the condition and you'll come out of the outer while loop. 这将破坏条件,您将退出外部while循环。

You should be setting a valid condition for the outer while loop if you want to continue. 如果要继续,则应该为外部while循环设置有效条件。 Try something like while(x < 5); 试试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 1)尝试将try catch块移动到内部循环中,以便仅将其围起来

int guess = input.nextInt();

2) your idea for number 2 should work. 2)您对第2条的想法应该可行。

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. 确保这是检查中的第一个if语句,那么您不必更改其他任何if语句。

3) make a variable outside both of the loops called guess, then instead of saying 3)在两个循环外都创建一个称为guess的变量,然后不要说

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. 4)您的变量x令人困惑。 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. 那么当您准备好退出循环时,可以将标志设置为false。 change 更改

x = 2;

to

flag = false;

also for the loop all change 也为循环所有变化

while(x==1)

to

while(flag)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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