【问题标题】:Basic Java: Error: Class, Interface, or Enum Expected [duplicate]基本 Java:错误:需要类、接口或枚举 [重复]
【发布时间】:2013-11-01 04:22:24
【问题描述】:

我正在尝试编写一个方法来查看字符串是否是回文(也可以正确拼写向后的单词,例如“racecar”。我找不到错误,所以也许另一双眼睛会有所帮助。这里是代码:

public boolean isPalindrome(String str){
  numberofQuestions++;
  int n = str.length();
  for( int i = 0; i < n/2; i++ )
  if (str.charAt(i) != str.charAt(n-i-1)) return false;
  return true;
}

编辑:错误截图:

开始上课:

public class Geek{
private String name;
private int numberofQuestions=0;

最终编辑:在其中一个方法中发现了一个额外的“{”。感谢大家的帮助!

【问题讨论】:

  • 我假设 numberofQuestions 已定义
  • numberofQuestions++的目的是什么?! #高度好奇
  • 没有明显的错误。尝试查看堆栈跟踪并找出哪段代码有错误。
  • R.J,这是一个计数器,用于跟踪菜单中使用的问题数量。
  • 向我们展示您的整个代码。

标签: java


【解决方案1】:

我敢打赌,这与缺少大括号或在开始此方法定义之前关闭类主体有关的大括号有关。

【讨论】:

  • 你赢了。完美的。谢谢
  • 你赢了。完美的。谢谢
  • 我不敢相信这个无用的答案是这么多年后被接受的。
【解决方案2】:

方法应该完全包含在一个类中

public class Geek {
    private String name;
    private int numberofQuestions = 0;

        public boolean isPalindrome(String str) {
            numberofQuestions++;
            int n = str.length();
            for (int i = 0; i < n / 2; i++)
                if (str.charAt(i) != str.charAt(n - i - 1))
                    return false;
            return true;
        }
    }

【讨论】:

  • 这会失败,因为numberofQuestions 没有初始化。
  • @Quincunx 只需要初始化局部变量。如果未显式初始化,字段将具有默认值。
  • @Quincunx numberofQuestions 将自动初始化为 0,因为它是一个类变量
  • 开始上课
  • @user2943817 那么请把课程贴出来让我们看看
【解决方案3】:

你必须检查你的花括号在循环、方法和类之后是否正确结束。

【讨论】:

    【解决方案4】:

    isPalindrome() 函数设为静态。

    这是一个示例:

    public class Sample {
    
        private static int numberofQuestions;
    
        public static void main(String[] args)
        {
            String str = "racecar";
            String str2 = "notpalindrome";
            boolean test = isPalindrome(str);
            boolean test2 = isPalindrome(str2);
            System.out.println(str + ": " + test);
            System.out.println(str2 + ": " + test2);
        }
    
        public static boolean isPalindrome(String str) {
            numberofQuestions++;
            int n = str.length();
            for (int i = 0; i < n / 2; i++)
                if (str.charAt(i) != str.charAt(n - i - 1))
                    return false;
            return true;
        }
    }
    

    输出:

    racecar: true
    notpalindrome: false
    

    【讨论】:

      【解决方案5】:

      我认为花括号有问题。你没有结束主类 Geek{ } 的括号。

      检查这个:

        public class Geek 
         {
              private String name;
              private int numberofQuestions = 0;
      
                  public boolean isPalindrome(String str) 
                  {
                      numberofQuestions++;
                      int n = str.length();
                      for (int i = 0; i < n / 2; i++)
                          if (str.charAt(i) != str.charAt(n - i - 1))
                              return false;
                      return true;
                  }
         }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-18
        • 1970-01-01
        • 2016-04-16
        • 2015-07-20
        相关资源
        最近更新 更多