标识符预期和非法启动类型的几个错误。困惑

问题描述 投票:0回答:3

整个程序应该进入组合锁并接受组合。这是我遇到问题的代码。

import java.util.*;  // needed for Scanner

public class CombinationLock extends Lock
{
   // Instance Variables
   private String combination;

   Scanner keyboard = new Scanner(System.in);

   System.out.println("Enter Combination --> ");
   String combo = keyboard.nextLine();

   if(combination = combo)
   {
    super.open();
   }

public String toString()
   {
    String str = super.toString() + "\n" +
                 "Combination = " + combination + "\n";  
    return str;
   }

public void setCombination()
{

}

public boolean getCombination()
{

}

public CombinationLock()
{
   super();    // call the default constructor of the Lock class
   combination = "";
}

public CombinationLock(String combo)
{
    super();
    combination = combo;
}


}

这些是我得到的错误

--------------------Configuration: <Default>--------------------
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:10: error: <identifier> expected
   System.out.println("Enter Combination  ");
                     ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:10: error: illegal start of type
   System.out.println("Enter Combination  ");
                      ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:13: error: illegal start of type
   if(combination = combo)
   ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:13: error: <identifier> expected
   if(combination = combo)
                 ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:13: error: ';' expected
   if(combination = combo)
                   ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:13: error: illegal start of type
   if(combination = combo)
                         ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:13: error: <identifier> expected
   if(combination = combo)
                          ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:14: error: ';' expected
   {
    ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:15: error: illegal start of type
    super.open();
         ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:18: error: class, interface, or enum expected
public String toString()
       ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:22: error: class, interface, or enum expected
    return str;
    ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:23: error: class, interface, or enum expected
   }
   ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:25: error: class, interface, or enum expected
public void setCombination()
       ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:30: error: class, interface, or enum expected
public boolean getCombination()
       ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:35: error: class, interface, or enum expected
public CombinationLock()
       ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:38: error: class, interface, or enum expected
   combination = "";
   ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:39: error: class, interface, or enum expected
}
^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:41: error: class, interface, or enum expected
public CombinationLock(String combo)
       ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:44: error: class, interface, or enum expected
    combination = combo;
    ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:45: error: class, interface, or enum expected
}
^
20 errors

Process completed.

我不确定是什么导致了这个问题。我试图找到其他答案,但没有一个与我的问题相关。

java compiler-errors identifier
3个回答
1
投票

Java语句必须出现在代码块中。所以在这种情况下,你在方法之外的方法中的代码需要被{}包围。

但看起来你正在尝试阅读输入等。理想情况下,你应该在这个类中创建一个main方法,在这个类中你创建一个CombinationLock实例并在main方法中读取输入。


1
投票

你需要纠正一些事情:

  1. 获取用户输入的代码应该在方法内部。 Java不允许在方法或块之外编写这样的逻辑。
  2. 如果条件没有正确实施。 Java期望if中的表达式返回一个boolean。然而,在=内使用的if算子导致String。对于字符串比较,我们应该在if内部使用string1.equals(string2)

1
投票

您正在函数之外编写语句。确保所有语句都在函数内部,并且所有实例变量都具有范围标识符。

具体来说,确保以下语句在函数内:

Scanner keyboard = new Scanner(System.in);

System.out.println("Enter Combination --> ");
String combo = keyboard.nextLine();

if(combination = combo)
{
    super.open();
}
© www.soinside.com 2019 - 2024. All rights reserved.