【问题标题】:Java InputMismatchException?Java 输入不匹配异常?
【发布时间】:2014-12-10 02:34:03
【问题描述】:

如果用户输入字符串而不是整数,我会尝试对我的代码进行异常处理。我的代码会将最大索引的位置交换为最小索引。你能试着和我一起纠正这个问题吗?

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

public class ArraySwap 
{
    static int h;
    static Scanner data = new Scanner(System.in);
    static int[] list = new int[10];
    public static void main(String[] args)throws InputMismatchException
    {
        System.out.println("Please enter 10 numbers: ");
        for(h = 0; h < list.length; h++)
        {
        try
            {
                list[h] = data.nextInt();
            }
        catch(InputMismatchException h)
            {
                System.out.println("Please re-enter 10 numbers as an exception " 
                    + h.toString());
                continue;
            }
        }
        swap();
    }
    public static void printArray(int[] list)
    {
        int counter;
        for(counter = 0; counter < list.length; counter++)
            System.out.print(list[counter] + " ");
    }
    public static int smallestIndex(int[] list)
    {
        int length1 = list.length;
        int counter;
        int minIndex = 0;

        for (counter = 1; counter < length1; counter++)
            if (list[minIndex] > list[counter])
                minIndex = counter;
        return minIndex;
    }
    public static int largestIndex(int[] list)
    {
        int length2 = list.length;
        int counter;
        int maxIndex = 0;

        for (counter = 1; counter < length2; counter++)
            if (list[maxIndex] < list[counter])
                maxIndex = counter;
        return maxIndex;
    }
    public static void swap()
    {
        System.out.print("List of elements: ");
        printArray(list);
        System.out.println();

        int min_index = smallestIndex(list);
        int max_index = largestIndex(list);
        int min_num = list[min_index];

        System.out.println("Largest element in list is: " 
                + list[max_index]);

        System.out.println("Smallest element in list is: " 
                + list[min_index]);

        min_num = list[min_index];
        list[min_index] = list[max_index];
        list[max_index] = min_num;

        System.out.print("Revised list of elements: ");
        printArray(list);
        System.out.println();
    }
}

【问题讨论】:

  • 很不清楚你在问什么。你能说得更具体一点吗?
  • 嗨,Kick,我要求在我的主程序中创建一个豁免,然后它将在显示 InputMismatchException 后继续。
  • 之后会发生什么?
  • 你的代码现在有什么问题?
  • 好的。我的程序从键盘读取 10 个整数,并将它们存储在一个数组中。它找到数组中最大值和最小值的位置(或索引),并将它们交换(将最大的元素移动到最小的位置,并将最小的元素移动到最大的位置)。我现在想弄清楚的是,如果我输入了一个字符串,如何创建一个 InputMismatchException。

标签: java arrays exception for-loop inputmismatchexception


【解决方案1】:

您已经在对整数输入进行异常处理:

   try
        {
            list[h] = data.nextInt();
        }
    catch(InputMismatchException h)
        {
            System.out.println("Please re-enter 10 numbers as an exception " 
                + h.toString());
            continue;
        }
    }

您的问题是,在您的 catch 块中,您将 InputMismatchException 对象命名为 h。这也是您的循环计数变量。改变它。

catch(InputMismatchException ex)
    {
        System.out.println("Please re-enter 10 numbers as an exception " 
            + ex.toString());
        continue;
    }

你的第二个问题是你的 catch 块中的 print 语句被自动作为下一个循环的 Scanner 输入。因此,一旦输入了错误字符串,程序就不允许再输入任何数字。您需要做的是首先使用 data.next() 来使用您的错误消息。

catch (InputMismatchException ex) {
                 System.out.print("Please re-enter 10 numbers as an exception "
                 + ex.toString());
                 data.next();

            }

【讨论】:

  • 非常感谢您回答 Aparna Sridhar。问题是当我运行程序时,输出如下。
  • 请输入 10 个数字:1 2 3 4 5 e 重新输入输入数字作为异常:java.util.InputMismatchException 重新输入输入数字作为异常:java.util.InputMismatchException 重新输入输入编号作为异常:java.util.InputMismatchException 重新输入输入编号作为异常:java.util.InputMismatchException 重新输入输入编号作为异常:java.util.InputMismatchException 元素列表:1 2 3 4 5 0 0 0 0 0 列表中的最大元素是:5 列表中的最小元素是:0 修改后的元素列表:1 2 3 4 0 5 0 0 0 0
  • 是的,这是因为您在 catch 块中打印出的错误在 for 循环的下一次迭代中被用作 Scanner 的输入。由于这是一个字符串,因此您会再次遇到相同的问题。使用 data.next() 将消耗此错误,然后您可以在下一次迭代中返回进行整数输入。如果要更正现有输入,请使用 list[h]=data.nextInt(); 在 catch 块中再次请求输入
  • 你太棒了!这行得通,但是,它只工作一次,它怎么能始终如一地工作?
  • 您能否详细说明它只工作一次的意思?你能给我你试过的输入吗?
猜你喜欢
  • 2013-05-24
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2012-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多