【问题标题】:Java: Exception Help Char and IntJava:异常帮助 Char 和 Int
【发布时间】:2014-10-15 15:52:19
【问题描述】:

好吧,我又被这段代码卡住了。

我需要输入一个不允许用户输入 0 的异常(因为以后不能除以 0)并且用户不能输入字母字符。我正在尝试显示消息,忽略错误的输入,然后循环以允许用户重试,直到他们输入可接受的数字。

这是我所拥有的:

package exceptionhandler;

/**
 *
 * @author Sarah
 */
import java.util.Scanner;

public class ExceptionHandler {

    /**
     * @param args
     *            the command line arguments10 10
     */
    public static void main(String[] args) throws NumberFormatException {
        Scanner in = new Scanner(System.in);
        Scanner input = new Scanner(System.in);


        System.out.print("Please enter ten values:");
        System.out.println();
    // Input the data into array from the user.
        double[ ] digit = new double[11];
        int sum = 0;
        //Declare an array
        try
        {
        for (int i = 1; i < digit.length; i++) {
            System.out.print("Value " + i + ": ");
            digit[i] = (double)in.nextInt();
            sum += (int)digit[i];
        }
        catch (NumberFormatException e)
        {
            System.out.println("You Can Only Enter Numbers!");
         }
        }

        System.out.println("Total Values in Array:"+ sum);
         // Calculate the sum and print the total


        System.out.println();
        System.out.println("Would you like to divide the values?");
        System.out.println("Yes or No to Exit the Program");
        String a = input.next();


         if(a.equalsIgnoreCase("yes")){   
                double [] divisionResult = new double[digit.length / 2];
    //Division array declared
                for (int i = 1; i < digit.length; i += 2)
                {
                double result = digit[i];
                if (result > digit[i + 1]) 
                result = result / digit[i + 1]; 
        else {
                result = digit[i + 1] / result;
            }
                divisionResult [i / 2] = result;
                    System.out.println(result);
                }
            }
        else if(a.equalsIgnoreCase("no")){
           System.exit(0);
        }
        }
}

我尝试过声明 throw 异常,然后尝试了 try..catch。但它无法识别捕获并尝试相互交谈......所以我知道我做错了什么,但我看不出它应该去哪里。

异常是否在正确的位置?我应该做点别的吗?我的例外写错了吗?然后我怎样才能继续防止零的输入以及重新抛出?

帮助?

【问题讨论】:

  • try 语句在这里没有意义,你需要在 catch 之前另一个“}”,而在 catch 块之后你不需要另一个“}”。就目前而言,您正在尝试在未完成 try 块的情况下捕获某些内容。

标签: java exception-handling try-catch throws


【解决方案1】:

它应该是InputMismatchException 而不是NumberFormatException 在输入字符时捕获异常,如果用户输入 0 和 deduct 1 的当前索引,则需要检查 0 for -loop 让用户再试一次。

样本:

for (int i = 1; i < digit.length; i++) {
        try {
            System.out.print("Value " + i + ": ");
            digit[i] = (double) in.nextInt();
            sum += (int) digit[i];
            if(digit[i] == 0.0)
            {
                System.out.println("You cant enter 0: try again");
                --i;
            }
        } catch (InputMismatchException e) {
            System.out.println("You Can Only Enter Numbers!");
            --i;
            in.nextLine(); //to consume the character
        }
    }

结果:

Please enter ten values:
Value 1: 0
You cant enter 0: try again
Value 1: asd
You Can Only Enter Numbers!
Value 1: 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 2023-03-28
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多