如何显示错误消息而不是Java异常?

huangapple 未分类评论53阅读模式
英文:

How to display error message instead of java exception?

问题

package guessNumber;

import java.util.Scanner;

public class GuessNumberApp {

    public static void main(String[] args) {
        final int LIMIT = 10;

        System.out.println("猜数字游戏!");
        System.out.println("我想一个1到" + LIMIT + "之间的数字。");
        System.out.println();

        // 获取一个1到限制之间的随机数
        double d = Math.random() * LIMIT; // d >= 0.0 且 < limit
        int number = (int) d;             // 将double转换为int
        number++;                        // int >= 1 且 <= limit

        // 准备从用户读取输入
        Scanner sc = new Scanner(System.in);
        int count = 1;

        while (true) {
            try {
                int guess = sc.nextInt();
                System.out.println("你猜的数字是:" + guess);

                if (guess < 1 || guess > LIMIT) {
                    System.out.println("你的猜测无效。");
                    continue;
                }

                if (guess < number) {
                    System.out.println("太低了。");
                } else if (guess > number) {
                    System.out.println("太高了。");
                } else {
                    System.out.println("你在第 " + count + " 次猜中了。\n");
                    break;
                }

                count++;
            } catch (java.util.InputMismatchException e) {
                System.out.println("输入格式错误,请输入一个数字。");
                sc.nextLine(); // 清除输入缓冲区中的无效输入
            }
        }

        System.out.println("再见!");
    }
}
英文:

I am trying to make a Guessing Game for a Java assignment, I have everything I need except exception handling, you see I'm trying to make it display an error message instead of displaying Exception in thread "main" java.util.InputMismatchException when someone tries to enter a numerical number in alphabetical form. The code I have is followed.
(I know I need a try and catch but I don't know what to put exactly.)

package guessNumber;


import java.util.Scanner;

public class GuessNumberApp {

	public static void main(String[] args) {
		final int LIMIT = 10;
		
        System.out.println(&quot;Guess the number!&quot;);
        System.out.println(&quot;I&#39;m thinking of a number from 1 to &quot; + LIMIT);
        System.out.println();

        // get a random number between 1 and the limit
        double d = Math.random() * LIMIT; // d is &gt;= 0.0 and &lt; limit
        int number = (int) d;             // convert double to int
        number++;                        // int is &gt;= 1 and &lt;= limit
		
        // prepare to read input from the user
		Scanner sc = new Scanner(System.in);
		int count = 1;
		
		
		
		while (true) {
			int guess = sc.nextInt();
	        System.out.println(&quot;You guessed: &quot; + guess);
	        
	        
	        if (guess &lt; 1 || guess &gt; LIMIT) {
	        	System.out.println(&quot;Your Guess is Invalid.&quot;);
	        	continue;
	        }
	        
	        if (guess &lt; number) {
	        	System.out.println(&quot;Too Low.&quot;);
	        } else if (guess &gt; number) {
	        	System.out.println(&quot;Too High.&quot;);
	        } else {
	        	System.out.println(&quot;You guessed it in &quot; + count + &quot; tries.\n&quot;);
	        	break;
	        }
	        
	        count++;
		}
        
        
        System.out.println(&quot;Bye!&quot;);
  
        
	}

}

答案1

得分: 1

请尝试这样做:

尝试 {
    int guess = sc.nextInt();
} 捕获(InputMismatchException e) {
    System.out.println("一些友好的错误消息");
    继续;
}

这将替换

int guess = sc.nextInt();
英文:

try something like that:

try {
    int guess = sc.nextInt();
} catch(InputMismatchException e) {
    System.out.println(&quot;some nice error message&quot;);
    continue;
}

This would replace

int guess = sc.nextInt();

huangapple
  • 本文由 发表于 2020年4月7日 03:50:59
  • 转载请务必保留本文链接:https://java.coder-hub.com/61067859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定