我一直遇到错误,但无论我做什么,我似乎都无法修复它们

时间:2016-01-26 21:52:24

标签: java compiler-errors

它不断出现我似乎无法解决的错误,因为它告诉我要修复它只是让它变得更糟或使它变得不那么有意义。这不是第一次发生这种情况,我的代码需要为我编译甚至获得作业的分数。我吓坏了,不知道如何修复它,这些似乎是我的常见错误。请帮忙。

错误是:

---- jGRASP exec:javac -g IntFun.java

IntFun.java:14:错误:找不到符号       num = in.nextInt();             ^

符号:变量   location:IntFun类 IntFun.java:66:错误:找不到符号       num = in.nextInt();       ^

符号:变量num   location:IntFun类 IntFun.java:66:错误:找不到符号       num = in.nextInt();             ^

符号:变量   location:IntFun类 IntFun.java:67:错误:找不到符号       if(num <0)           ^

符号:变量num   location:IntFun类 IntFun.java:73:错误:找不到符号          System.out.print(&#34;您输入的数字是:&#34; + num);                                                          ^

符号:变量num   location:IntFun类 IntFun.java:79:错误:找不到符号       if(num == 0)           ^

符号:变量num   location:IntFun类 IntFun.java:81:错误:找不到符号       而(num> 0)              ^

符号:变量num   location:IntFun类 IntFun.java:83:错误:找不到符号          digit = num%10;                  ^

符号:变量num   location:IntFun类 IntFun.java:84:错误:找不到符号          num = num / 10;          ^

符号:变量num   location:IntFun类 IntFun.java:84:错误:找不到符号          num = num / 10;                ^

符号:变量num   location:IntFun类 IntFun.java:95:错误:找不到符号       for(int i = 2; i&lt; = num; i ++)                            ^

符号:变量num   location:IntFun类 IntFun.java:98:错误:变量j已在方法primeNum()中定义          for(int j = 2; j&lt; i; j ++)                   ^

IntFun.java:118:错误:找不到符号       while(num&gt; = 0)              ^

符号:变量num   location:IntFun类 IntFun.java:120:错误:找不到符号          digit = num%10;                  ^

符号:变量num   location:IntFun类 IntFun.java:122:错误:找不到符号          num = num / 10;          ^

符号:变量num   location:IntFun类 IntFun.java:122:错误:找不到符号          num = num / 10;                ^

符号:变量num   location:IntFun类 IntFun.java:124:错误:不兼容的类型:意外的返回值       回报;              ^

17个错误

---- jGRASP wedge2:进程的退出代码为1。  ---- jGRASP:操作完成。

这是我的代码:

import java.util.Scanner;

public class IntFun
{
   public static void main(String[] args)
   {
      int num = 0;
      Scanner kb = new Scanner(System.in);
      System.out.print("Enter a number: ");
      num = in.nextInt();

      while (num >= 0)
      {
         int option;
         option = displayMenu(kb);

         while (option != 6)
         {
            switch (option)
            {
               case 1:
                  enterNum();
                  System.out.print("Entering a new number.");
                  break;
               case 2:
                  oddsEvens();
                  System.out.println("Printing the number of odd digits, even digits, and zeroes in the integer.");
                  break;
               case 3:
                  primeNum();
                  System.out.println("Printing the prime numbers between 2 and the integer...");
                  break;
               case 4:
                  sumDigit();
                  System.out.println("Printing the sum of the digits of the integer...");
               case 5:
                  System.out.println("Good Bye!");
                  System.exit(1);
            }
            option = displayMenu(kb);
         }
      }
   }

   private static int displayMenu(Scanner kb)
   {
      int myOption = 0;
      while (myOption != 1 && myOption != 2 && myOption != 3 && myOption != 4 && myOption != 5)
      {
         System.out.println("\t\t1. Enter a new number.\n\t\t2. Print the number of odd digits, even digits, and zeroes in the integer.\n\t\t3. Print the prime numbers between 2 and the integer.\n\t\t4. Print the sum of the digits of the integer.\n\t\t5. Quit");
         myOption = kb.nextInt();
       kb.nextLine();
         if (!(myOption == 1 || myOption == 2 || myOption == 3 || myOption == 4 || myOption == 5))
            System.out.println("Invalid choice");
      }
      return myOption;
   }

   private static void enterNum()
   {
      System.out.print("Enter New Number: ");
      num = in.nextInt();
      if (num < 0)
      {
         System.out.print("New Number not accepted, please try again.");
      }
      else
      {
         System.out.print("The number you entered is:" + num);
      }
   }
   private static void oddsEvens()
   {
      int odds = 0, evens = 0, zeros = 0, digit;
      if (num == 0)
         zeros++;
      while (num > 0)
      {
         digit = num % 10;
         num = num / 10;
         if (digit == 0)
            zeros++;
         else if (digit % 2 == 0)
            evens++;
         else if (digit % 2 == 1)
            odds++;
      }
   }
   private static void primeNum()
   {
      for (int i = 2; i <= num; i++)
      {
         int j;
         for (int j = 2; j < i; j++)
         {
            int n = i % j;
            if (n == 0)
            {
               break;
            }
         }
         if (i == j)
         {
            System.out.print(" "+i);
         }                
         System.out.println();
      }
   }
   private static void sumDigit()
   {
      int sum = 0;
      int digit =0;

      while (num >= 0)
      {
         digit = num % 10;
         sum = sum + digit;
         num = num / 10;
      }
      return sum;
   } 
}

1 个答案:

答案 0 :(得分:5)

您需要在任何其他人的范围之外声明您的方法:

import java.util.Scanner;

public class IntFun {
    public static void main(String[] args) {
        // get your methods out of here
    }

    private static int displayMenu(Scanner kb) {
        ...
    }

    private static void enterNum() {
        ...
    }

    private static void oddsEvens() {
        ...
    }

    private static void primeNum() {
        ...
    }

    private static void sumDigit() {
        ...
    }
}