英文:
How to reject characters/ letters in a java program
问题
当程序要求用户输入10个元素(数字),但用户输入任何字母或单词时,我希望程序显示“无效输入”。我尝试了一个if语句,但它不起作用。非常感谢任何帮助!
这是我的代码:
public static void main(String[] args) {
int[] Array = new int[10];
int index;
Scanner input = new Scanner(System.in);
System.out.println("输入10个元素:");
for (int i = 0; i < 10; i++) {
if (input.hasNextInt()) {
Array[i] = input.nextInt();
} else {
System.out.println("无效输入");
break;
}
}
System.out.print("输入要检索的索引:");
index = input.nextInt();
System.out.print("索引 " + index + " 处的元素是 " + Array[index]);
}
注意:在代码中,我添加了一个判断语句来检查输入是否为整数。如果输入不是整数,则会显示“无效输入”。
英文:
When the program asks the user to input 10 elements (numbers), but the user entered any letter or word, I want the program to display "INVALID INPUT". I tried an if statement but it doesn't work. Any help is much appreciated!
This is my code:
int [] Array = new int[10];
int index;
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 elements:");
for (int i = 0; i < 10; i++) {
Array[i] = input.nextInt();
}
System.out.print("Enter an index you want to retrieve: ");
index = input.nextInt();
System.out.print("Element at index "+index+" is " + Array[index]);
}
}
</details>
# 答案1
**得分**: 2
你可以使用 `try-catch` 来捕获 `InputMismatchException`,然后可以相应地处理它。
像这样:
```java
try {
int[] Array = new int[10];
int index;
Scanner input = new Scanner(System.in);
System.out.println("输入 10 个元素:");
for (int i = 0; i < 10; i++) {
Array[i] = input.nextInt();
}
System.out.print("输入要检索的索引:");
index = input.nextInt();
System.out.print("索引 " + index + " 处的元素为 " + Array[index]);
} catch (InputMismatchException e) {
System.out.println("无效输入");
}
例如,当我输入:
spectric 不酷
我会得到:
无效输入
因为 spectric 很酷。
英文:
You can use a try-catch
and catch a InputMismatchException
, where you can then handle it accordingly.
Like so:
try{
int [] Array = new int[10];
int index;
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 elements:");
for (int i = 0; i < 10; i++) {
Array[i] = input.nextInt();
}
System.out.print("Enter an index you want to retrieve: ");
index = input.nextInt();
System.out.print("Element at index "+index+" is " + Array[index]);
}catch(InputMismatchException e){
System.out.println("INVALID INPUT");
}
For example, when I input:
spectric is not cool
I get:
INVALID INPUT
Because spectric is cool
答案2
得分: 2
尝试以下操作:您需要捕获输入不匹配的情况,然后继续接受输入。重要的是要更新循环计数器,以便接受所有 10 个输入。
int[] Array = new int[10];
int index;
Scanner input = new Scanner(System.in);
System.out.println("输入 10 个元素:");
for (int i = 0; i < 10; i++) {
try {
Array[i] = input.nextInt();
} catch (InputMismatchException ie) {
System.out.println("输入无效 - 请重新输入");
i--; // 更新循环
input.nextLine(); // 清除缓冲区中的无效输入
}
}
System.out.print("输入您想要检索的索引:");
index = input.nextInt();
System.out.print("索引 " + index + " 处的元素为 " + Array[index]);
英文:
Try the following: You need to catch the input mismatch and then continue taking input. It is important to update the loop counter to allow for all 10 inputs to be taken.
int [] Array = new int[10];
int index;
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 elements:");
for (int i = 0; i < 10; i++) {
try {
Array[i] = input.nextInt();
} catch(InputMismatchException ie) {
System.out.println("Bad input - please re-enter");
i--; // update loop;
input.nextLine();// clear bad input from buffer scanner.
}
}
System.out.print("Enter an index you want to retrieve: ");
index = input.nextInt();
System.out.print("Element at index "+index+" is " + Array[index]);
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论