java - 在 kb.nextInt() 中找不到符号;

标签 java java.util.scanner

我正在为我的类(class)编写一个程序,我想对其进行测试。但是,我无法编译它,因为我在“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);

 }
}

这是唯一阻止我运行程序并查看我的代码是否正确编写的部分。

谢谢你的帮助。

最佳答案

你只需更改

menuChoice = kb.nextlnt();

menuChoice = kb.nextInt();

那么你的代码就可以正常工作了。

关于java - 在 kb.nextInt() 中找不到符号;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33183379/

相关文章:

java - 如何在顶部移动列表的最后一个元素 N 次

java - Java 是否以某种方式支持 .htaccess 或者有其他选择吗?

java - MapReduce 作业挂起

java - 如何使用 Builder 类绑定(bind) config.jelly 上的复选框?

java - 将十进制转换为二进制 Java

java - 按扩展名列出和计数文件

Java 的 Scanner vs String.split() vs StringTokenizer;我应该使用哪个?

string - 在 while 循环中使用 hasNextInput() 它为给定的每个字符串提供循环,但我希望它提示一次

java - 确保我的 RNG 不会根据用户输入字符串崩溃

java - 如何在java中读取文本文件并将行分配给变量