我正在为我的班级编写一个程序,我想测试它。但是,我无法编译它,因为我在“menuChoice = kb.nextInt();”中收到此错误“错误:无法找到符号”
import java.util.*;
public class Program3
{
public static void main (String [ ] args)
{
Scanner kb = new Scanner(System.in);
System.out.println("Please enter a non-negative integer.: ");
int num = kb.nextInt();
Random rand = new Random();
int sum = 0;
int factor = 1;
int menuChoice;
while (num > 0)
{
System.out.println("Number cannot be negative! Please enter a non-negative integer.: ");
num = kb.nextInt();
}
do
{
System.out.println("\nPlease choose an option:");
System.out.println("\t0\tPrint the number");
System.out.println("\t1\tDetermine if the number is odd or even");
System.out.println("\t2\tFind the reciprocal of the number");
System.out.println("\t3\tFind half of the number");
System.out.println("\t4\tRaise the number to the power of 5 (using a Java method)");
System.out.println("\t5\tRaise the number to the power of 5 (using a loop)");
System.out.println("\t6\tGenerate 20 random numbers between 0 and the number (inclusive)");
System.out.println("\t7\tFind the sum of 0 up to your number (using a loop)");
System.out.println("\t8\tFind the factorial of the number (using a loop)");
System.out.println("\t9\tFind the square root of the number (using a Java method)");
System.out.println("\t10\tFind the square root of the number (using a loop, Extra Credit)");
System.out.println("\t11\tDetermine whether the number is prime (using a loop, Extra Credit) ");
System.out.println("\t12\tExit the program");
menuChoice = kb.nextlnt(); //<<< error occurs right here!!!
switch (menuChoice)
{
case 0: System.out.print("Your number is" + num);
break;
case 1: if (num % 2 ==0)
System.out.print(num + " is even");
else
System.out.print(num + " is false");
break;
case 2: if (num == 0)
System.out.print("There are no reciprocal");
else
System.out.print("The reciprocal of " + num + " is 1/" + num);
break;
case 3: System.out.print("half of " + num + " is" + (num/2));
break;
case 4: System.out.print(num + " of the power of 5 is" + (Math.pow(num , 5)));
break;
case 5: for (int i = 1 ; i <= 6 ; i++)
for (int j = 1 ; j <= 6 ; j ++)
System.out.print(num + " of the power of 5 is" + ( i * j));
break;
case 6: for (int i = 1 ; i<=20; i++)
System.out.println(rand.nextInt(6));
break;
case 7: for (int i = 1; i <=num ; i++)
{
sum += 1;
System.out.println(sum);
}
break;
case 8: for (int i = 1 ; i <=num; i++)
factor = factor*i;
System.out.println(factor);
break;
case 9: double theSqrt = Math.sqrt(num);
System.out.println("The square root of " + num + " is " + theSqrt);
break;
case 10: System.out.println("Did not do this extra credit :(");
break;
case 11: boolean isPrime;
for (int i = 2; i<=num; i++)
{
isPrime = true;
for (int divisor = 2; divisor<Math.sqrt(num) && isPrime; divisor++)
{
if (num%divisor == 0)
isPrime = false;
}
if (isPrime)
System.out.println("The prime number of" + num + " is prime");
}
break;
case 12: System.out.println("Exiting the now.");
break;
default: System.out.println("Illegal choice, try again");
break;
}
}while (menuChoice !=12);
}
}
这是阻止我运行程序并查看我的代码是否被正确编写的唯一部分。
感谢您的帮助。
答案 0 :(得分:2)
你只需要改变
menuChoice = kb.nextlnt();
到
menuChoice = kb.nextInt();
然后您的代码将正常运行。
答案 1 :(得分:1)
您的代码中有拼写错误。扫描仪中没有像nextlnt
这样的方法。
menuChoice = kb.nextlnt();
^
您应将其更改为:
menuChoice = kb.nextInt();
^
同时查看条件:
while (num > 0)
这意味着如果num大于0,那么您将其视为负数。所以相反,你应该拥有:
while (num < 0)