//我知道坐标arnt右侧修复它们后我可以让它得到遵守但我无法弄清楚为什么它一直给我这个消息
public class DiceRoller
Public static void main(String[] args
{
if (dice1 == 1)
StdDraw.square(.5, .4, .2);
StdDraw.circle(.3, .5, .035);
else if (dice1 == 2)
StdDraw.square(.5, .4, .2);
StdDraw.circle(.3, .45, .035);
StdDraw.circle(.3, .55, .035);
else if (dice1 == 3)
StdDraw.square(.5, .4, .2);
StdDraw.circle(.3, .55, .035);
StdDraw.circle(.3, .45, .035);
StdDraw.circle(.3, .50, .035);
else if (dice1 == 4)
StdDraw.square(.5, .4, .2);
StdDraw.circle(.25, .55, .035);
StdDraw.circle(.25, .40, .035);
StdDraw.circle(.75, .55, .035);
StdDraw.circle(.75, .40, .035);
else if (dice1 == 5)
StdDraw.square(.5, .4, .2);
StdDraw.circle(.25, .55, .035);
StdDraw.circle(.25, .40, .035);
StdDraw.circle(.75, .55, .035);
StdDraw.circle(.75, .55, .035);
StdDraw.circle(.3, .5, .035);
else if (dice1 == 6)
StdDraw.square(.5, .4, .2);
StdDraw.circle(.25, .50, .035);
StdDraw.circle(.25, .60, .035);
StdDraw.circle(.25, .40, .035);
StdDraw.circle(.75, .60, .035);
StdDraw.circle(.75, .40, .035);
StdDraw.circle(.75, .50, .035);
if (dice2 == 1)
StdDraw.square(.5, .7, .2);
StdDraw.circle(.3, .5, .035);
else if (dice2 == 2)
StdDraw.square(.5, .7, .2);
StdDraw.circle(.3, .45, .035);
StdDraw.circle(.3, .55, .035);
else if (dice2 == 3)
StdDraw.square(.5, .7, .2);
StdDraw.circle(.3, .55, .035);
StdDraw.circle(.3, .45, .035);
StdDraw.circle(.3, .50, .035);
else if (dice2 == 4)
StdDraw.square(.5, .7, .2);
StdDraw.circle(.25, .55, .035);
StdDraw.circle(.25, .40, .035);
StdDraw.circle(.75, .55, .035);
StdDraw.circle(.75, .40, .035);
else if (dice2 == 5)
StdDraw.square(.5, .7, .2);
StdDraw.circle(.25, .55, .035);
StdDraw.circle(.25, .40, .035);
StdDraw.circle(.75, .55, .035);
StdDraw.circle(.75, .55, .035);
StdDraw.circle(.3, .5, .035);
else if (dice2 == 6)
StdDraw.square(.5, .7, .2);
StdDraw.circle(.25, .50, .035);
StdDraw.circle(.25, .60, .035);
StdDraw.circle(.25, .40, .035);
StdDraw.circle(.75, .60, .035);
StdDraw.circle(.75, .40, .035);
StdDraw.circle(.75, .50, .035);
}
答案 0 :(得分:3)
您需要将if
放在括号{}
中,如下所示:
if (dice1 == 2)
{ //should be enclosed since there are more than one statement to be executed
StdDraw.square(.5, .4, .2);
StdDraw.circle(.3, .45, .035);
StdDraw.circle(.3, .55, .035);
}
答案 1 :(得分:1)
错误:'else'没有'if
if (dice1 == 1)
{
StdDraw.square(.5, .4, .2);
StdDraw.circle(.3, .5, .035);
}
else if (dice1 == 2)
{
StdDraw.square(.5, .4, .2);
StdDraw.circle(.3, .45, .035);
StdDraw.circle(.3, .55, .035);
}
................
应该在括号中。因为它包含多个语句。
答案 2 :(得分:1)
使用花括号
开始和结束每个if和else-if语句答案 3 :(得分:1)
显然,你混淆了Python和Java。在Python中,您通过缩进来指示一个块,但这对Java不起作用。在Java中,您使用花括号({}
)来表示代码块。这意味着您应该在代码中添加大括号:
if (dice1 == 1) {
StdDraw.square(.5, .4, .2);
StdDraw.circle(.3, .5, .035);
} else if (dice1 == 2) {
StdDraw.square(.5, .4, .2);
StdDraw.circle(.3, .45, .035);
StdDraw.circle(.3, .55, .035);
} else if (blah blah blah) {
blah blah blah
} ...
现在您的代码应该正常运行。你问,"为什么这个错误并没有预期' {}期待'或类似的东西?"。那是因为如果你用Java编写这段代码:
if (dice1 == 1)
StdDraw.square(.5, .4, .2);
StdDraw.circle(.3, .5, .035);
else if (dice1 == 2)
StdDraw.square(.5, .4, .2);
StdDraw.circle(.3, .45, .035);
StdDraw.circle(.3, .55, .035);
实际上意味着:
if (dice1 == 1) {
StdDraw.square(.5, .4, .2);
}
StdDraw.circle(.3, .5, .035);
else if (dice1 == 2) {
StdDraw.square(.5, .4, .2);
}
StdDraw.circle(.3, .45, .035);
StdDraw.circle(.3, .55, .035);
现在你明白为什么它说你没有if语句。
答案 4 :(得分:0)
只要“then”子句只包含一个语句
,左右括号就是可选的
您必须将if
和else if
块括在大括号{}
中。
if (dice1 == 1) { // <---- braces
StdDraw.square(.5, .4, .2);
StdDraw.circle(.3, .5, .035);
} // <---- braces
else if (dice1 == 2) {
...
}