我从一个网站上获得了这个代码以适应其他用途,但是当我在Netbeans上尝试它时,它一直给我一些问题.我看了看它看起来很稳固,但我显然不是......例外是:
"'{'预期"和"类,界面或枚举预期."
我检查了所有括号和括号,我无法弄清楚它有什么问题.
public class Coin()
{
private String sideUp;
/**
* Default constructor
*/
public Coin()
{
// initialize sideUp
toss();
}
/**
* This method will simulate the tossing of a coin. It should set
* the
* sideUp field to either "heads" or "tails".
*/
public void toss()
{
Random rand = new Random();
// Get a random value, 0 or 1.
int value = rand.nextInt(2);
if (value == 0)
{
this.sideUp = "heads";
}
else
{
this.sideUp = "tails";
}
}
/**
*
* @return The side of the coin facing up.
*/
public String getSideUp()
{
return sideUp;
}
}
Run Code Online (Sandbox Code Playgroud)
有什么支撑我在某处丢失吗?
你需要从类名中删除括号,它应该是:
public class Coin
{
...
}
Run Code Online (Sandbox Code Playgroud)
在Java语言规范中不包括括号cf§3.8的标识符中仅允许" Java字母 "或" Java数字 " .
| 归档时间: |
|
| 查看次数: |
37 次 |
| 最近记录: |