Java输入不匹配异常?
如果用户输入字符串而不是整数,我会尝试在代码上执行一个异常。我的代码将把最大索引的位置交换到最小索引。你能和我一起纠正一下吗Java输入不匹配异常?,java,arrays,exception,for-loop,inputmismatchexception,Java,Arrays,Exception,For Loop,Inputmismatchexception,如果用户输入字符串而不是整数,我会尝试在代码上执行一个异常。我的代码将把最大索引的位置交换到最小索引。你能和我一起纠正一下吗 import java.util.Scanner; import java.util.InputMismatchException; public class ArraySwap { static int h; static Scanner data = new Scanner(System.in); static int[] list = n
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();
}
}
import java.util.Scanner;
导入java.util.InputMismatchException;
公共类ArraySwap
{
静态int-h;
静态扫描仪数据=新扫描仪(System.in);
静态int[]列表=新int[10];
publicstaticvoidmain(字符串[]args)抛出inputmatchException
{
System.out.println(“请输入10个数字:”);
对于(h=0;h列表[counter])
minIndex=计数器;
返回minIndex;
}
公共静态int最大索引(int[]列表)
{
int length2=list.length;
整数计数器;
int maxIndex=0;
用于(计数器=1;计数器<长度2;计数器++)
if(list[maxIndex]
您已经在整数输入上执行异常处理:
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语句将自动作为下一个循环的扫描仪输入。因此,一旦输入错误字符串,程序就不允许再输入任何数字。您需要做的是首先使用data.next()来使用错误消息
catch (InputMismatchException ex) {
System.out.print("Please re-enter 10 numbers as an exception "
+ ex.toString());
data.next();
}
很不清楚你在问什么。你能说得更具体一点吗?嗨,Kick,我要求在我的主程序中创建一个豁免,然后它将在显示InputMismatchException后继续。之后会发生什么?你的代码现在有什么问题?好的。我的程序从键盘读取10个整数,并将它们存储在一个数组中。它查找数组中最大值和最小值的位置(或索引),并交换它们(将最大的元素移动到最小的位置,将最小的元素移动到最大的位置)。我现在想弄清楚的是,如果我输入了字符串,如何创建InputMismatchException。非常感谢您回答AparnaSridhar。问题是,当我运行程序时,输出如下。请输入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 0 0 0 0 0列表中最大的元素是:5列表中最小的元素是:0修订的元素列表:1 2 3 4 0 5 0 0 0 0 0 0 0是,因此发生这种情况是因为您在catch块中打印出的错误是在for循环的下一次迭代中作为扫描仪的输入。因为这是一个字符串,所以您会再次遇到相同的问题。使用data.next()将使用此错误,然后您可以在下一次迭代中返回进行整数输入。如果要更正现有输入,请使用list[h]=data.nextInt()在catch块中再次请求输入;你真棒!但是,它只起作用一次,它怎么能始终如一地起作用?你能详细解释一下你所说的“它只起作用一次”是什么意思吗?你能给我你试过的输入吗?