预期的标识符错误和类型非法启动。困惑 [英] Several errors for identifier expected and illegal start of type. Confused

查看:78
本文介绍了预期的标识符错误和类型非法启动。困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

整个程序应该输入密码锁并接受密码。这是我遇到的代码。

The whole program is supposed to enter a combination lock and accept the combo. This is the code I'm having issues with.

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.

我不确定是什么引起了问题。我试图找到其他答案,但都与我的问题无关。

I'm not sure what's causing the problem. I've tried to find other answers but none of them were relevant to my problem.

推荐答案

您要在函数外部编写语句。确保所有语句都在函数内部,并且所有实例变量都具有作用域标识符。

You are writing statements outside of a function. Make sure all statements are inside functions, and that all instance variables have scope identifiers.

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

Specifically, make sure the following statements are inside a function:

Scanner keyboard = new Scanner(System.in);

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

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

这篇关于预期的标识符错误和类型非法启动。困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆