繁体   English   中英

如何清除此错误?

[英]How do I clear this error?

我正在使用Java,并且我的程序应要求用户输入数字1-10。 如果数字不在护林员中,则它会显示一条错误消息。 还应该在拼云中显示所选的号码。 我的错误来自整数编号= 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;



        }


    }
}

您要重新声明number两次。 摆脱顶部的第一个声明。 这个:

int number;

您正在尝试定义两次int数。

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

int number = keyboard.nextInt();

number = keyboard.nextInt();

您可能还希望将调用包装在try and catch中,以检查是否有来自Scanner.nextInt()方法的预期异常。

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

这将行不通:它将永远打印消息,因为数字值在此循环内没有改变。

好,首先

我的错误来自整数编号= keyboard.nextInt();

好吧,很高兴知道您在哪一行上遇到错误,但是哪种错误会更好,只需将错误消息复制到问题中,可能variable number is already defined或类似的东西

这行可能还会出现另一个错误,如果您输入的不是整数, InputMismatchException可能会引发InputMismatchException

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

这不会更改number的值,请更改为

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;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM