我的 else if 语句在我的井字游戏程序中不起作用
my else if statement isnt working in my tic tac toe program
//主要代码
public static void main(String[] args) {
//启动游戏面板
initGame();
//start the game
do {
PlayerMove(Cplayer);
updategame(Cplayer, crow, ccol);
printboard();
//game over message
if (currentstate == you_win){
System.out.println("'X' Won!!");
else if (currentstate == comp_win)
System.out.println("'O' won!!");
else if (currentstate == draw)
System.out.println("It's a Draw :(");
}
if (currentstate == you_win){
System.out.println("'X' Won!!");
else if (currentstate == comp_win)
System.out.println("'O' won!!");
else if (currentstate == draw)
System.out.println("It's a Draw :(");
}
看看这一行(我删除了一些无关代码以使其更容易看到):
if (currentstate == you_win){
else if (currentstate == comp_win)
//...
}
这样看,你能看出为什么这是不正确的吗?顺便说一句,这就是为什么您应该始终使用 {
和 }
的原因。像这样执行 if
...else
:
if (currentstate == you_win){
// ...
}
else if (currentstate == comp_win) {
// ...
}
//...
或者,更好的是,只使用 switch
语句:
switch (currentstate) {
case you_win:
System.out.println("'X' Won!!");
break;
case comp_win:
System.out.println("'O' won!!");
break;
case draw:
System.out.println("It's a Draw :(");
break;
}
//主要代码
public static void main(String[] args) {
//启动游戏面板
initGame();
//start the game
do {
PlayerMove(Cplayer);
updategame(Cplayer, crow, ccol);
printboard();
//game over message
if (currentstate == you_win){
System.out.println("'X' Won!!");
else if (currentstate == comp_win)
System.out.println("'O' won!!");
else if (currentstate == draw)
System.out.println("It's a Draw :(");
}
if (currentstate == you_win){
System.out.println("'X' Won!!");
else if (currentstate == comp_win)
System.out.println("'O' won!!");
else if (currentstate == draw)
System.out.println("It's a Draw :(");
}
看看这一行(我删除了一些无关代码以使其更容易看到):
if (currentstate == you_win){
else if (currentstate == comp_win)
//...
}
这样看,你能看出为什么这是不正确的吗?顺便说一句,这就是为什么您应该始终使用 {
和 }
的原因。像这样执行 if
...else
:
if (currentstate == you_win){
// ...
}
else if (currentstate == comp_win) {
// ...
}
//...
或者,更好的是,只使用 switch
语句:
switch (currentstate) {
case you_win:
System.out.println("'X' Won!!");
break;
case comp_win:
System.out.println("'O' won!!");
break;
case draw:
System.out.println("It's a Draw :(");
break;
}