2015-10-17 78 views
0

我正在为我的班级写一个程序,我想测试一下。然而,我无法编译它,因为我得到这个错误“错误:无法找到符号”在“menuChoice = kb.nextInt();”在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); 

} 
} 

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

谢谢你的帮助。

回答

2

你只需要改变的

menuChoice = kb.nextlnt(); 

menuChoice = kb.nextInt(); 

那么你的代码将正常工作。

+0

和你的情况也是错的。 改变条件为while(num <0) –

+0

谢谢大声笑我没有看到。 – Paincakes

1

您的代码中存在拼写错误。 Scanner中没有像nextlnt这样的方法。

menuChoice = kb.nextlnt(); 
        ^

您应将其更改为:

menuChoice = kb.nextInt(); 
        ^

而且看这个while条件:

while (num > 0) 

这意味着如果num大于0,那么你把它当作负数。所以,你应该让你喜欢:

while (num < 0) 
+0

感谢您的帮助,非常感谢! – Paincakes