这是我想要制作的游戏。你以$ 1500开始游戏,每次你想要玩游戏你必须支付100美元。(选择1来玩)(按2阅读规则)(按3看高分)游戏是一个翻转硬币游戏。当比赛开始时,底池中有4美元。当它开始时,底池中的钱会翻倍,然后再次翻转。当它尾巴时,你可以获得所有的钱。当你的价格低于100美元时,你就会破产(游戏结束)。你回到起始菜单......我希望这个游戏记录你的最高钱(高分) 所以这就是我到目前为止所得到的......
public class COINFLIP
{
public static void main(String[] args)
{
int tail = 0;//tail
int head = 0;//head
int x = 0;//tries
while (x < 1)
{
int coin;//coin
coin = (int) (Math.random() * 2);
x++;
if (coin == 0) {
headCount++; // int type
wasHeads = true; // boolean type
wasTails = false;
} else {
tailCount++;
wasHeads = false; // boolean type
wasTails = true;
}
int sm = 1500;//starting money
int cm;//current money
int pm = 4;//money in the pot
cm = sm - 100 + pm;
}
if (wasHeads)
{
pm = pm * 2;
}
else if (wasTails)
{
System.out.println("Your money is: $" + cm);
}
if (cm < 100);
System.out.println("Sorry, but you are broke!!!");
}
}
错误:
C:\Users\Loyal Xie\Documents\COINFLIP.java:14: error: cannot find symbol
headCount++; // int type
^
symbol: variable headCount
location: class COINFLIP
C:\Users\Loyal Xie\Documents\COINFLIP.java:15: error: cannot find symbol
wasHeads = true; // boolean type
^
symbol: variable wasHeads
location: class COINFLIP
C:\Users\Loyal Xie\Documents\COINFLIP.java:16: error: cannot find symbol
wasTails = false;
^
symbol: variable wasTails
location: class COINFLIP
C:\Users\Loyal Xie\Documents\COINFLIP.java:18: error: cannot find symbol
tailCount++;
^
symbol: variable tailCount
location: class COINFLIP
C:\Users\Loyal Xie\Documents\COINFLIP.java:19: error: cannot find symbol
wasHeads = false; // boolean type
^
symbol: variable wasHeads
location: class COINFLIP
C:\Users\Loyal Xie\Documents\COINFLIP.java:20: error: cannot find symbol
wasTails = true;
^
symbol: variable wasTails
location: class COINFLIP
C:\Users\Loyal Xie\Documents\COINFLIP.java:27: error: cannot find symbol
if (wasHeads)
^
symbol: variable wasHeads
location: class COINFLIP
C:\Users\Loyal Xie\Documents\COINFLIP.java:27: error: illegal start of type
if (wasHeads)
^
C:\Users\Loyal Xie\Documents\COINFLIP.java:29: error: cannot find symbol
pm = pm * 2;
^
symbol: variable pm
location: class COINFLIP
C:\Users\Loyal Xie\Documents\COINFLIP.java:29: error: cannot find symbol
pm = pm * 2;
^
symbol: variable pm
location: class COINFLIP
C:\Users\Loyal Xie\Documents\COINFLIP.java:31: error: cannot find symbol
else if (wasTails)
^
symbol: variable wasTails
location: class COINFLIP
C:\Users\Loyal Xie\Documents\COINFLIP.java:31: error: illegal start of type
else if (wasTails)
^
C:\Users\Loyal Xie\Documents\COINFLIP.java:33: error: cannot find symbol
System.out.println("Your money is: $" + cm);
^
symbol: variable cm
location: class COINFLIP
C:\Users\Loyal Xie\Documents\COINFLIP.java:35: error: cannot find symbol
if (cm < 100);
^
symbol: variable cm
location: class COINFLIP
14 errors
但是有很多错误,我的陈述仍然是错误的...我认为问题在if语句中,我认为我仍然无法设置head = true然后在if语句中使用它。 ......有什么想法吗?
答案 0 :(得分:0)
如果您更正了缩进,您可能会注意到
if (coin == 0)
{
headCount++; // int type
}
wasHeads = true; // boolean type
wasHeads
不在if语句中,并且将在100%的时间内执行。 wasTails = true;
这意味着稍后您的if (wasHeads)
测试将始终执行。
另外,为什么在while
循环中有这个,因为它只执行一次。
修改强>
关于您的错误,您需要先定义变量才能使用它们。
例如
headCount++;
需要首先定义为
int headCount = 0;
答案 1 :(得分:0)
您对布尔值的分配不在各自的测试用例中(您应该更喜欢简单的else
)。等等,
if (coin == 0) {
headCount++; // int type
wasHeads = true; // boolean type
wasTails = false;
} else {
tailCount++;
wasHeads = false; // boolean type
wasTails = true;
}