我正在使用Java,我的程序应该要求用户输入数字1-10。如果该号码不在游侠中,则假定显示错误消息。它也应该在Pinyun中显示所选的数字。我的错误来自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;
}
}
}
答案 0 :(得分:3)
您重新宣布number
两次。摆脱顶部的第一个声明。这一个:
int number;
答案 1 :(得分:2)
您尝试定义int
个数字的两倍。
删除第一个声明或更改行:
int number = keyboard.nextInt();
与
number = keyboard.nextInt();
您可能还希望在try和catch中包装调用以检查来自Scanner.nextInt()方法的预期异常。
答案 2 :(得分:2)
int number = keyboard.nextInt();
while (number < 1 || number > 10)
System.out.print("Error, choose a number between 1-10");
这不起作用:它只会打印消息,因为数字值在此循环内没有变化。
答案 3 :(得分:0)
好的,首先要做的事情。
我的错误来自int number = keyboard.nextInt();
嗯,很高兴知道哪一行你得到错误,但什么错误会更好,只需将错误信息复制到问题,可能variable number is already defined
或类似的东西
此行可能有另一个错误,如果您键入的内容不是整数,则可以使用InputMismatchException
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;
说真的吗?编程是懒惰人的发明者,所以可以告诉计算机做它们的工作,尽管这样的编程是可能的,尽量避免这样的假设(在更多地方使用相同的代码)。< / p>