gpt4 book ai didi

java - 如何在我的程序中使用 try/catch/finally 过滤扫描仪输入错误?

转载 作者:行者123 更新时间:2023-12-01 19:48:30 25 4
gpt4 key购买 nike

我写了这个小猜谜游戏,你必须猜测一个随机数。我完成了这个程序,但我想做的最后一件事是:当用户输入一个数字及其和InputMissmatching错误或其他错误时,系统告诉他必须输入一个大于0的数字。但是 Scanner 的变量在这个 do-while 循环之后不再起作用了!如何使用循环设置 try/catch/finally 来仅获取整数,并且当它不是整数或 0 时不会崩溃?

问题是guessNumber和numberOfUser变量现在还没有初始化!行:37,53 那么我必须在 try/catch 循环中更改什么?

package gessinggame;

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

public class GessingGame {

public static void main(String[] args) {




//<editor-fold desc="Start Variables and Objects">

Random random = new Random();
Scanner input = new Scanner(System.in);

int counter = 0;
int numberOfUser;
int guessNumber;
int x = 1;
System.out.print("Try to guess my number! What should be the highest number I can go up to?: ");
do{
try{
numberOfUser = input.nextInt();
guessNumber = random.nextInt(numberOfUser);
x = 2;
}catch(Exception e)
{
System.out.print("That doesn't work! Please enter an integer higher than 0 :");
x = 1;
}
}while(x == 1);
//</editor-fold>

//<editor-fold desc="do-while loop (higher/lower/equal)">

System.out.println("Try to guess my number! Its from 0 to " + numberOfUser + " !");
System.out.println("---------------------------------------------------");

do{

int userguess = input.nextInt();

if(userguess > numberOfUser)
{
System.out.println("Oh man, at the beginning you said that the highest number is: " + numberOfUser);
}
else{
if(userguess < 0)
{
System.out.println("My number is always taller than 0");
} else{
if(guessNumber < userguess)
{
System.out.println("lower");
counter++;
}
if(guessNumber > userguess)
{
System.out.println("higher");
counter++;
}
if(userguess == guessNumber)
{
counter++;
break;
}
}
}
}while(true);

//</editor-fold>

//<editor-fold desc="print final result">

System.out.println("You guessed my number! My number was: " + guessNumber + "! You needed " + counter + " attempts!");

//</editor-fold>
}

}

最佳答案

try/catch block 中添加 input.nextLine()
在每个 Scanner.nextInt 之后调用 Scanner.nextLine 来消耗该行的其余部分,包括换行符。

 package gessinggame;

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

public class GessingGame {

public static void main(String[] args) {




//<editor-fold desc="Start Variables and Objects">

Random random = new Random();
Scanner input = new Scanner(System.in);

int counter = 0;
int numberOfUser = 0;
int guessNumber = 0;
int x = 1;
System.out.print("Try to guess my number! What should be the highest number I can go up to?: ");
do{
try{
numberOfUser = input.nextInt();
guessNumber = random.nextInt(numberOfUser);
x = 2;
}catch(Exception e)
{
System.out.print("That doesn't work! Please enter an integer higher than 0 :");
input.nextLine();
x = 1;
}
}while(x == 1);


System.out.println("Try to guess my number! Its from 0 to " + numberOfUser + " !");
System.out.println("---------------------------------------------------");

do{

int userguess = input.nextInt();

if(userguess > numberOfUser)
{
System.out.println("Oh man, at the beginning you said that the highest number is: " + numberOfUser);
}
else{
if(userguess < 0)
{
System.out.println("My number is always taller than 0");
} else{
if(guessNumber < userguess)
{
System.out.println("lower");
counter++;
}
if(guessNumber > userguess)
{
System.out.println("higher");
counter++;
}
if(userguess == guessNumber)
{
counter++;
break;
}
}
}
}while(true);
System.out.println("You guessed my number! My number was: " + guessNumber + "! You needed " + counter + " attempts!");
}

}

关于java - 如何在我的程序中使用 try/catch/finally 过滤扫描仪输入错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59103328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com