Java:异常帮助Char和Int

Java:异常帮助Char和Int,java,exception-handling,try-catch,throws,Java,Exception Handling,Try Catch,Throws,好吧,我又被这个代码卡住了 我需要输入一个不允许用户输入0的异常,因为您不能稍后除以0,并且用户不能输入字母字符。我试图显示消息,忽略错误的输入,并循环以允许用户重试,直到他们输入可接受的数字 以下是我所拥有的: package exceptionhandler; /** * * @author Sarah */ import java.util.Scanner; public class ExceptionHandler { /** * @param args

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

我需要输入一个不允许用户输入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);
        }
        }
}
我已尝试声明抛出异常,然后尝试尝试尝试捕获。但这并不是说要抓住对方并试着互相交谈。。。所以我知道我做错了什么,但我看不出它应该去哪里

例外情况是否在正确的位置?我应该做点别的吗?我的例外写错了吗?那么,我如何才能继续阻止零的输入,以及-再触发

帮忙

输入字符时捕获异常应该是InputMismatchException而不是NumberFormatException,如果用户输入0,则需要检查0,并扣除for循环的当前索引1,以便用户重试

样本:


try语句在这里没有意义,在catch之前需要另一个},在catch块之后不需要另一个}。就目前情况而言,您正在尝试在未完成try块的情况下捕获某些内容。
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: