我试图添加一个尝试捕获,告诉用户他们不能插入负数 [英] Im trying to add a try catch that tells the user they cant plug in negative numbers

查看:141
本文介绍了我试图添加一个尝试捕获,告诉用户他们不能插入负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够添加一个尝试捕获,告诉用户他们不能使用
字母。但是由于某种原因添加一个尝试捕获负数量dosent似乎工作。我知道try块是在哪里如果somthing可能会出错,比如输入一个负数,catch可以打印出错误信息。我认为这就是我的问题所在。与try catch相关的另一个问题是我使用输入-1的用户输入用户输入的内容,所以我认为它会导致逻辑问题。

I was able to add a try catch that tells the user that they cant use letters.However for some reason adding a try catch for negative numbers dosent seem to work.I know that the try block is where if somthing can go wrong like entering in a negative number the catch can print out the error message. I think thats where my problem lies. Another problem that is associated with the try catch is that I'm use to the user entering in -1 to enter the contents that the user inputs so I'm thinking its gonna cause a logical problem.

tl; dr添加try catch或其他catch以防止用户添加负数

tl;dr Adding a try catch or another catch to prevent user from adding negative numbers

这不是整个程序,但它做的是它过滤掉用户输入的整数,并分离平均值和赔率。

this is not the the whole program but what it does is that it filters out the integers that the user inputs and separates the evens and odds.

 public static void main(String [] args)
    {
        Scanner stdin = new Scanner(System.in);//for user input
        int[] evenNum = new int [100];//Even Array up too 100
        int[] oddNum = new int[100];//Odd Array up too 100
        int evenIndex=0;//even numbers
        int input=0;//user input
        int i=0;//incrementer for arrays
        int k=0; 
        int j=0;
        String name;

        System.out.println("Type In Your Name");//Type in name 
            name = stdin.nextLine();


while ((i < oddNum.length && i < evenNum.length) && input !=-1)//100 numbers only
        {
    try{//this is what we want anything else the catch will block it and display a message


                System.out.println(name+" Enter a positive number, Enter -1 For results");
                input= stdin.nextInt();

                oddNum[i]=input;//holds input
                i++;//Increments array
            }  

                catch(Exception d){
                    System.out.println("Only Positive Numbers & no Letters Please!");
                    stdin.next();

                     }


                }


推荐答案

从扫描仪获取后,您可以检查输入变量

You can check the input variable after you get it from the scanner

if (input < 0) {
     System.out.println("Only Positive Numbers & no Letters Please!");
}

从扫描仪读取数字时,您的代码不会抛出任何异常。因此,当您输入负数时,您不能指望执行会跳转到catch块。

Your code does not throw any Exception when the number is read from the scanner. So you cannot expect that the execution jumps into the catch-block when you enter a negative number.

但是当您在时可以抛出异常输入为负数。这将使线程直接跳转到catch块。在catch-block中,您可以打印传递的消息 IllegalArgumentException

But you can alternatively throw an exception when the input is negative. This will make the thread to jump directly into the catch-block. In the catch-block you can then print the message you passed the IllegalArgumentException

if (input < 0) {
     // this gets caught in the catch block
     throw new IllegalArgumentException("Only Positive Numbers & no Letters Please!"); 
}      
...
} catch (IllegarArgumentException e) {
    System.out.println(e.getMessage());
}

捕捉异常通常是不好的做法 java.lang.Exception )。这是所有已检查异常的根,只要抛出 Exception 的任何子类,就会跳转到catch块。

你期待的具体例外。 (在这种情况下 IllegalArgumentException 。)

It is generally bad practice to catch Exception (java.lang.Exception). This is the "root" of all checked exceptions and the catch-block will be jumped into whenever any subclass of Exception is thrown.
Just catch the concrete exception that you are expecting. (In this case IllegalArgumentException.)

此外,您不应使用异常来控制程序的执行流程。

Also you should not use exceptions to control the execution flow of your program.

我会建议这样的事情:

do {
    System.out.println(name+" Enter a positive number, Enter -1 For results");
    try {
        input = stdin.nextInt();
    } catch (java.util.InputMismatchException e) { // if the user enters something that is not an integer
        System.out.println("Please only enter integers");
        input = Integer.MIN_VALUE; 
        stdin.next(); // consume the non-int so we don't get caught in an endless loop
    }
} while (input < -1);  // loop as long as the input is less than -1

if (input == -1) {
    // show the results here
}

这将接受正整数并将提示输入,直到用户输入正数0(零)或-1 (应显示结果)

This will accept positive integers and will prompt for an input until the user enters a positive number, 0 (zero) or -1 (which should show the results)

这篇关于我试图添加一个尝试捕获,告诉用户他们不能插入负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆