首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java InputMismatchException?

Java InputMismatchException?
EN

Stack Overflow用户
提问于 2014-12-10 10:34:04
回答 1查看 1.1K关注 0票数 1

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

代码语言:javascript
复制
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();
    }
}
EN

回答 1

Stack Overflow用户

发布于 2014-12-10 11:03:17

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

代码语言:javascript
复制
   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。这也是循环计数变量。改变这一切。

代码语言:javascript
复制
catch(InputMismatchException ex)
    {
        System.out.println("Please re-enter 10 numbers as an exception " 
            + ex.toString());
        continue;
    }

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

代码语言:javascript
复制
catch (InputMismatchException ex) {
                 System.out.print("Please re-enter 10 numbers as an exception "
                 + ex.toString());
                 data.next();

            }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27392432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档