如何在 Java 中允许越界索引
How to allow out of bounds index in Java
我尝试制作程序,以便用户可以输入月份的整数(例如,当用户输入数字 4 时,输出应该是四月)并且它会一直询问用户输入有效时间号码 (1 - 12)。如果用户输入无效数字,程序应说“无效!”,然后终止。但是,我的程序无法通过 while 循环并立即将无效数字设置为异常。我应该怎么做才能使程序显示“无效!”?谢谢!
String[] months = new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "Oktober", "November", "December"};
Scanner input = new Scanner(System.in);
int i = 1;
while(i < 13 && i > 0)
{
if(i > 12 && i < 1)
{
break;
}
else
{
System.out.println("Month?");
i = input.nextInt();
System.out.println(months[i - 1] + "\n");
}
}
System.out.println("Invalid!");
你应该检查数组边界是否在 0 到 months.length-1 之间,因为数组索引从 0 开始。应该避免捕获 indexOutOfbounds,它表明代码有问题。
i = input.nextInt();
if(i-1 < 0 || i-1 >= months.length)
{
break;
}
else
{
System.out.println(months[i - 1] + "\n");
}
您的 if 条件始终为假,因为数字不能同时 > 12 和 i < 1。请改用 i > 12 || i < 1
。
访问数组时出现异常,可以先读取下一个数字,然后检查,如果数字有效才访问数组。
经过(最少)更正的列表将是:
String[] months = new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "Oktober", "November", "December"};
Scanner input = new Scanner(System.in);
int i = 1;
while(i < 13 && i > 0)
{
System.out.println("Month?");
i = input.nextInt();
if(i > 12 || i < 1)
{
break;
}
else
{
System.out.println(months[i - 1] + "\n");
}
}
System.out.println("Invalid!");
您有 2 个选项。
您将数组访问包装到一个 try-catch 块中并捕获异常,然后打印“无效”。
你坚持以下几点:首先要求输入,然后检查是否满足约束条件,只有满足约束条件,才访问数组。然后在循环中继续这样做,直到不再满足约束。 (更好的选择)
考虑选项 2 的这个例子:
public static void main(String[] args) {
String[] months = new String[] { "January", "February", "March", "April", "May", "June", "July", "August", "September",
"Oktober", "November", "December" };
Scanner input = new Scanner(System.in);
System.out.println("Month?");
int i = input.nextInt();
while (i < 13 && i > 0) {
System.out.println(months[i - 1] + "\n");
System.out.println("Month?");
i = input.nextInt();
}
System.out.println("Invalid!");
}
不需要 if 子句和 break,因为 while 循环会完成所有工作。
您可以使用无限循环(即while(true){}
)并在 InputMismatchException
或输入整数超出有效范围(即 1
到 [=19] 的情况下中断它=]).
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[] months = new String[] { "January", "February", "March", "April", "May", "June", "July", "August",
"September", "Oktober", "November", "December" };
Scanner input = new Scanner(System.in);
int i;
while (true) {
System.out.print("Month?");
try {
i = input.nextInt();
if (i >= 1 && i <= 12) {
System.out.println(months[i - 1]);
} else {
System.out.println("Invalid");
break;
}
} catch (InputMismatchException e) {
System.out.println("Invalid");
break;
}
}
}
}
样本运行:
Month?10
Oktober
Month?1
January
Month?15
Invalid
另一个样本运行:
Month?12
December
Month?1
January
Month?abc
Invalid
通过使用 java.time
API:
import java.time.DateTimeException;
import java.time.Month;
import java.time.format.TextStyle;
import java.util.InputMismatchException;
import java.util.Locale;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true) {
System.out.print("Month?");
try {
Month month = Month.of(input.nextInt());
// Change the locale as per your requirement e.g. Locale.ENGLISH
System.out.println(month.getDisplayName(TextStyle.FULL, Locale.GERMAN));
} catch (DateTimeException | InputMismatchException e) {
System.out.println("Invalid");
break;
}
}
}
}
样本运行:
Month?10
Oktober
Month?1
Januar
Month?15
Invalid
另一个样本运行:
Month?10
Oktober
Month?1
Januar
Month?Abc
Invalid
你应该尝试这样的事情:------
(如果你不想打破循环)
public static void main(String[] args) {
String[] months = new String[] { "January", "February", "March", "April", "May", "June", "July", "August",
"September", "Oktober", "November", "December" };
Scanner input = new Scanner(System.in);
while (true) {
try {
System.out.println("Month?");
int inputFromUser = input.nextInt();
if (inputFromUser < 13 && inputFromUser > 0)
{
System.out.println(months[inputFromUser - 1]);
} else {
System.out.println("Invalid! input");
System.out.println("Do you want to come out from loop yes/no");
if (input.next().equalsIgnoreCase("yes")) {
break;
}
}
} catch (InputMismatchException e) {
System.out.println("Invalid! input , please provide the correct integer input");
break;
}
}
input.close();
}
如果您无法打破循环,请尝试以下逻辑:--
首先使用“if”检查输入,然后应用 while 循环。
好吧,您正在使用相同的 i 变量进行数组索引,这也是您收到异常的原因,因为在退出 while 循环以输入无效输入之前,必须执行 System.out.println(months[i - 1] + "\n");
语句,因此如果数组索引 months[value]
中的值大于数组大小,您将收到 ArrayIndexOutBoundsException
的异常,并且如果索引值小于 0
为了避免这种情况,只需在输入语句 i = input.nextInt();
之后添加 if(i>12 || i<1) break;
System.out.println("Month?");
i = input.nextInt();
if(i>12 || i<1)
break;
System.out.println(months[i - 1] + "\n");
所以在输入大于 12 或小于 1 的数字后,while 循环将中断
System.out.println("Invalid!");
将执行
我尝试制作程序,以便用户可以输入月份的整数(例如,当用户输入数字 4 时,输出应该是四月)并且它会一直询问用户输入有效时间号码 (1 - 12)。如果用户输入无效数字,程序应说“无效!”,然后终止。但是,我的程序无法通过 while 循环并立即将无效数字设置为异常。我应该怎么做才能使程序显示“无效!”?谢谢!
String[] months = new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "Oktober", "November", "December"};
Scanner input = new Scanner(System.in);
int i = 1;
while(i < 13 && i > 0)
{
if(i > 12 && i < 1)
{
break;
}
else
{
System.out.println("Month?");
i = input.nextInt();
System.out.println(months[i - 1] + "\n");
}
}
System.out.println("Invalid!");
你应该检查数组边界是否在 0 到 months.length-1 之间,因为数组索引从 0 开始。应该避免捕获 indexOutOfbounds,它表明代码有问题。
i = input.nextInt();
if(i-1 < 0 || i-1 >= months.length)
{
break;
}
else
{
System.out.println(months[i - 1] + "\n");
}
您的 if 条件始终为假,因为数字不能同时 > 12 和 i < 1。请改用 i > 12 || i < 1
。
访问数组时出现异常,可以先读取下一个数字,然后检查,如果数字有效才访问数组。
经过(最少)更正的列表将是:
String[] months = new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "Oktober", "November", "December"};
Scanner input = new Scanner(System.in);
int i = 1;
while(i < 13 && i > 0)
{
System.out.println("Month?");
i = input.nextInt();
if(i > 12 || i < 1)
{
break;
}
else
{
System.out.println(months[i - 1] + "\n");
}
}
System.out.println("Invalid!");
您有 2 个选项。
您将数组访问包装到一个 try-catch 块中并捕获异常,然后打印“无效”。
你坚持以下几点:首先要求输入,然后检查是否满足约束条件,只有满足约束条件,才访问数组。然后在循环中继续这样做,直到不再满足约束。 (更好的选择)
考虑选项 2 的这个例子:
public static void main(String[] args) {
String[] months = new String[] { "January", "February", "March", "April", "May", "June", "July", "August", "September",
"Oktober", "November", "December" };
Scanner input = new Scanner(System.in);
System.out.println("Month?");
int i = input.nextInt();
while (i < 13 && i > 0) {
System.out.println(months[i - 1] + "\n");
System.out.println("Month?");
i = input.nextInt();
}
System.out.println("Invalid!");
}
不需要 if 子句和 break,因为 while 循环会完成所有工作。
您可以使用无限循环(即while(true){}
)并在 InputMismatchException
或输入整数超出有效范围(即 1
到 [=19] 的情况下中断它=]).
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[] months = new String[] { "January", "February", "March", "April", "May", "June", "July", "August",
"September", "Oktober", "November", "December" };
Scanner input = new Scanner(System.in);
int i;
while (true) {
System.out.print("Month?");
try {
i = input.nextInt();
if (i >= 1 && i <= 12) {
System.out.println(months[i - 1]);
} else {
System.out.println("Invalid");
break;
}
} catch (InputMismatchException e) {
System.out.println("Invalid");
break;
}
}
}
}
样本运行:
Month?10
Oktober
Month?1
January
Month?15
Invalid
另一个样本运行:
Month?12
December
Month?1
January
Month?abc
Invalid
通过使用 java.time
API:
import java.time.DateTimeException;
import java.time.Month;
import java.time.format.TextStyle;
import java.util.InputMismatchException;
import java.util.Locale;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true) {
System.out.print("Month?");
try {
Month month = Month.of(input.nextInt());
// Change the locale as per your requirement e.g. Locale.ENGLISH
System.out.println(month.getDisplayName(TextStyle.FULL, Locale.GERMAN));
} catch (DateTimeException | InputMismatchException e) {
System.out.println("Invalid");
break;
}
}
}
}
样本运行:
Month?10
Oktober
Month?1
Januar
Month?15
Invalid
另一个样本运行:
Month?10
Oktober
Month?1
Januar
Month?Abc
Invalid
你应该尝试这样的事情:------
(如果你不想打破循环)
public static void main(String[] args) {
String[] months = new String[] { "January", "February", "March", "April", "May", "June", "July", "August",
"September", "Oktober", "November", "December" };
Scanner input = new Scanner(System.in);
while (true) {
try {
System.out.println("Month?");
int inputFromUser = input.nextInt();
if (inputFromUser < 13 && inputFromUser > 0)
{
System.out.println(months[inputFromUser - 1]);
} else {
System.out.println("Invalid! input");
System.out.println("Do you want to come out from loop yes/no");
if (input.next().equalsIgnoreCase("yes")) {
break;
}
}
} catch (InputMismatchException e) {
System.out.println("Invalid! input , please provide the correct integer input");
break;
}
}
input.close();
}
如果您无法打破循环,请尝试以下逻辑:--
首先使用“if”检查输入,然后应用 while 循环。
好吧,您正在使用相同的 i 变量进行数组索引,这也是您收到异常的原因,因为在退出 while 循环以输入无效输入之前,必须执行 System.out.println(months[i - 1] + "\n");
语句,因此如果数组索引 months[value]
中的值大于数组大小,您将收到 ArrayIndexOutBoundsException
的异常,并且如果索引值小于 0
为了避免这种情况,只需在输入语句 i = input.nextInt();
if(i>12 || i<1) break;
System.out.println("Month?");
i = input.nextInt();
if(i>12 || i<1)
break;
System.out.println(months[i - 1] + "\n");
所以在输入大于 12 或小于 1 的数字后,while 循环将中断
System.out.println("Invalid!");
将执行