我从网站上获取此代码以适应其他用途,但是当我在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;
}
}
我在某处遗失了支撑吗?
答案 0 :(得分:5)
您需要从类名中删除括号,而应该是:
public class Coin
{
...
}
在Java语言规范中不包含括号cf §3.8的标识符中仅允许“ Java letters ”或“ Java digits ”。
答案 1 :(得分:0)
从类名中删除()并执行必要的导入。
可行的代码将是这样的:
import java.util.Random;
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;
}
}