与 try/catch InputMismatch 异常作斗争并从数组中检索数据
Struggling with try/catch InputMismatch Exception and retrieving data from an array
我正在尝试执行以下操作:
- 调用一个方法来创建一个
Array
具有 100 个随机生成的整数。
- 调用提示用户输入数组索引位置和returns输入值的方法。如果用户输入的值不是有效整数,则抛出一个 "Input Mismatch Exception" 提示用户问题并允许他们有机会输入正确的整数或退出程序。
package latest;
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.Random;
public class Latest
{
private static int[] randomInteger;
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int indexPosition = 0;
randomInteger = new int[100];
Random rand = new Random();
for (int i=0; i<randomInteger.length;i++)
randomInteger[i] = rand.nextInt();
while (indexPosition < 0 || indexPosition > 100)
{
try
{
// get array index position
System.out.println ("Hello, please enter an integer for the array index position: ");
indexPosition = input.nextInt();
}
catch (InputMismatchException e)
{
System.out.println ("You did not input a valid value. Please enter an Integer value between 0 and 100");
indexPosition = input.nextInt();
}
{
System.out.println ("You did not input a valid value. Please enter an Integer value between 0 and 100");
indexPosition = input.nextInt();
System.out.println (randomInteger[indexPosition]);
}
}
}
}
我的问题是代码编译但不输出任何内容,IDE 说 indexPosition - "Assigned value never used"
编辑:如果您输入有效的整数,评论中的 ingrid 代码可以完美运行,但它不会捕获任何异常,它只会崩溃
你快到了。试试下面的版本。
package latest;
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.Random;
public class Latest {
private static int[] randomInteger;
public static void main(String[] args) {
int indexPosition = 0;
Scanner input = new Scanner(System.in);
randomInteger = new int[100];
Random rand = new Random();
for (int i = 0; i < randomInteger.length; i++)
randomInteger[i] = rand.nextInt();
System.out
.println("Hello, please enter an integer for the array index position: ");
do {
try {
// get array index position
indexPosition = input.nextInt();
} catch (InputMismatchException e) {
System.out
.println("You did not input a valid value. Please enter an Integer value between 0 and 100");
input.nextLine();
continue;
} catch (Exception e) {
System.out
.println("You did not input a valid value. Please enter an Integer value between 0 and 100");
input.nextLine();
continue;
}
if (indexPosition < 0 || indexPosition > 100) {
System.out
.println("You did not input a valid value. Please enter an Integer value between 0 and 100");
}
} while (indexPosition < 0 || indexPosition > 100);
System.out.println(randomInteger[indexPosition]);
input.close();
}
}
我正在尝试执行以下操作:
- 调用一个方法来创建一个
Array
具有 100 个随机生成的整数。 - 调用提示用户输入数组索引位置和returns输入值的方法。如果用户输入的值不是有效整数,则抛出一个 "Input Mismatch Exception" 提示用户问题并允许他们有机会输入正确的整数或退出程序。
package latest;
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.Random;
public class Latest
{
private static int[] randomInteger;
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int indexPosition = 0;
randomInteger = new int[100];
Random rand = new Random();
for (int i=0; i<randomInteger.length;i++)
randomInteger[i] = rand.nextInt();
while (indexPosition < 0 || indexPosition > 100)
{
try
{
// get array index position
System.out.println ("Hello, please enter an integer for the array index position: ");
indexPosition = input.nextInt();
}
catch (InputMismatchException e)
{
System.out.println ("You did not input a valid value. Please enter an Integer value between 0 and 100");
indexPosition = input.nextInt();
}
{
System.out.println ("You did not input a valid value. Please enter an Integer value between 0 and 100");
indexPosition = input.nextInt();
System.out.println (randomInteger[indexPosition]);
}
}
}
}
我的问题是代码编译但不输出任何内容,IDE 说 indexPosition - "Assigned value never used"
编辑:如果您输入有效的整数,评论中的 ingrid 代码可以完美运行,但它不会捕获任何异常,它只会崩溃
你快到了。试试下面的版本。
package latest;
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.Random;
public class Latest {
private static int[] randomInteger;
public static void main(String[] args) {
int indexPosition = 0;
Scanner input = new Scanner(System.in);
randomInteger = new int[100];
Random rand = new Random();
for (int i = 0; i < randomInteger.length; i++)
randomInteger[i] = rand.nextInt();
System.out
.println("Hello, please enter an integer for the array index position: ");
do {
try {
// get array index position
indexPosition = input.nextInt();
} catch (InputMismatchException e) {
System.out
.println("You did not input a valid value. Please enter an Integer value between 0 and 100");
input.nextLine();
continue;
} catch (Exception e) {
System.out
.println("You did not input a valid value. Please enter an Integer value between 0 and 100");
input.nextLine();
continue;
}
if (indexPosition < 0 || indexPosition > 100) {
System.out
.println("You did not input a valid value. Please enter an Integer value between 0 and 100");
}
} while (indexPosition < 0 || indexPosition > 100);
System.out.println(randomInteger[indexPosition]);
input.close();
}
}