Java 如何清除此错误?

Java 如何清除此错误?,java,Java,我使用的是Java,我的程序应该要求用户输入数字1-10。如果号码不在ranger中,它将显示一条错误消息。此外,它还应该显示选定的数字在云端。我的错误来自int number=keyboard.nextInt 以下是我的其余代码: import java.util.Scanner; public class ForReal { public static void main(String[] args) { int number; Scanner

我使用的是Java,我的程序应该要求用户输入数字1-10。如果号码不在ranger中,它将显示一条错误消息。此外,它还应该显示选定的数字在云端。我的错误来自int number=keyboard.nextInt

以下是我的其余代码:

import java.util.Scanner;
public class ForReal {


    public static void main(String[] args) {

        int number;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("enter an number");
        int number = keyboard.nextInt();


        while (number < 1 || number > 10) 
            System.out.print("Error, choose a number between 1-10");

        switch (number)
        {
        case 1 : System.out.println("The English numberal 1" + number + " converts to the pinyin numeral yil1"); 
                                                    break;

        case 2:  System.out.println("The English numeral 2" + number + " converts to the pinyin numeral er4");  
                                                    break;

        case 3: System.out.println("The English numeral 3" + number + " converts to the pinyin numeral san1");
                                                    break;
        case 4: System.out.println("The English numeral 4" + number + " converts to the pinyin numeral si4");
                                                    break;
        case 5: System.out.println("The English numeral 5" + number + " converts to the pinyin numeral wu3");
                                                    break;
        case 6: System.out.println("The English numeral 6" + number + " converts to the pinyin numeral liu4");
                                                    break;
        case 7: System.out.println("The English numeral 7" + number + " converts to the pinyin numeral qil");
                                                    break;
        case 8: System.out.println("The English numeral 8" + number + " converts to the pinyin numeral bal");
                                                    break;
        case 9: System.out.println("The Arabic numeral 9" + number + " converts to the pinyin numeral jiu3");
                                                    break;
        case 10: System.out.println("The Arabic numeral 10" + number + " converts to the pinyin numeral shi2");
                                                    break;



        }


    }
}

您试图定义两倍的整数

删除第一个声明或更改行:

int number = keyboard.nextInt();

您可能还希望将调用包装为try-and-catch,以检查来自该方法的预期异常。

您声明了两次数字。去掉顶部的第一个声明。这个:

int number;

这将不起作用:它将永远打印消息,因为这个循环中的数字值不会改变。

好的,首先要做的事情

我的错误来自int number=keyboard.nextInt

很高兴知道你在哪一行得到了错误,但是什么错误会更好,只要把错误信息复制到问题上,可能变量号已经定义好了或者类似的东西

这一行可能还有另一个错误,如果您键入的内容不是整数,则InputMismatchException可能为thorwn

while (number < 1 || number > 10) 
System.out.print("Error, choose a number between 1-10");

真的吗?编程是懒人的发明者,因此可以告诉计算机让他们工作,尽管这样编程是可能的,但要尽量避免重复,在更多这样的地方使用相同的代码。

这是int number=keyboard.nextInt;您的问题已得到回答,但为了将来参考,您将始终希望告诉我们完整的错误消息以及导致错误的行。我们能够更好、更快、更容易地理解您的问题,我们就能够更好、更快地提供帮助。1+并且始终将每个if块和循环括在花括号内,甚至单行循环内,尤其是在早期。
int number = keyboard.nextInt();
while (number < 1 || number > 10) 
    System.out.print("Error, choose a number between 1-10");
while (number < 1 || number > 10) 
System.out.print("Error, choose a number between 1-10");
while (number < 1 || number > 10) {
    System.out.print("Error, choose a number between 1-10");
    number = keyboard.nextInt();
}
case 1 : System.out.println("The English numberal 1" + number + " converts to the pinyin numeral yil1"); 
     break;