如何在Java程序中拒绝字符/字母

huangapple go评论78阅读模式
英文:

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!

如何在Java程序中拒绝字符/字母

This is my code:

        int [] Array = new int[10];
        int index;

        Scanner input = new Scanner(System.in);

            System.out.println(&quot;Enter 10 elements:&quot;);

            for (int i = 0; i &lt; 10; i++) {
                Array[i] = input.nextInt();
            }

        System.out.print(&quot;Enter an index you want to retrieve: &quot;);
        index = input.nextInt();

        System.out.print(&quot;Element at index &quot;+index+&quot; is &quot; + 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(&quot;Enter 10 elements:&quot;);
        for (int i = 0; i &lt; 10; i++) {
            Array[i] = input.nextInt();
        }
        System.out.print(&quot;Enter an index you want to retrieve: &quot;);
        index = input.nextInt();
        System.out.print(&quot;Element at index &quot;+index+&quot; is &quot; + Array[index]);
    }catch(InputMismatchException e){
        System.out.println(&quot;INVALID INPUT&quot;);
    }

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(&quot;Enter 10 elements:&quot;);

     for (int i = 0; i &lt; 10; i++) {
       	try {
           Array[i] = input.nextInt();
       	} catch(InputMismatchException ie) {
       		System.out.println(&quot;Bad input - please re-enter&quot;);
       		i--; // update loop;
       		input.nextLine();// clear bad input from buffer scanner.
       	}
     }

   System.out.print(&quot;Enter an index you want to retrieve: &quot;);
   index = input.nextInt();

   System.out.print(&quot;Element at index &quot;+index+&quot; is &quot; + Array[index]);

</details>



huangapple
  • 本文由 发表于 2020年9月25日 08:27:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64056136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定