java结束程序exit,java - exit(0)不会终止我的Java代码中的程序执行。 为什么? - 堆栈内存溢出...

public class TicTacToe{

public static void main(String[] args){

System.out.println("X starts the game");

Scanner sc = new Scanner(System.in);

char a[][] = new char[3][3];

int n=0;

int i = 0;

int j = 0;

while(n<9){

System.out.println("Enter the coordinates but not" +i +" " +j);

int x = sc.nextInt();

int y = sc.nextInt();

System.out.println("Enter X");

a[x][y]=sc.next().charAt(0);

n++;

if(n>3){

if(a[0][0]=='X' && a[0][1]== 'X' && a[0][2]=='X'){

System.out.println("Congrats! X won");

exit(0);

}

else if(a[0][0]=='X' && a[1][0]== 'X' && a[2][0]=='X'){

System.out.println("Congrats! X won");

exit(0);

}

else if(a[1][0]=='X' && a[1][1]== 'X' && a[1][2]=='X'){

System.out.println("Congrats! X won");

exit(0);

}

else if(a[0][1]=='X' && a[1][1]== 'X' && a[2][1]=='X'){

System.out.println("Congrats! X won");

exit(0);

}

else if(a[2][0]=='X' && a[2][1]== 'X' && a[2][2]=='X'){

System.out.println("Congrats! X won");

exit(0);

}

else if(a[0][2]=='X' && a[1][2]== 'X' && a[2][2]=='X'){

System.out.println("Congrats! X won");

exit(0);

}

else if(a[0][0]=='X' && a[1][1]== 'X' && a[2][2]=='X'){

System.out.println("Congrats! X won");

exit(0);

}

else if(a[0][2]=='X' && a[1][1]== 'X' && a[2][0]=='X'){

System.out.println("Congrats! X won");

exit(0);

}

}

System.out.println("Enter the coordinates but not" +x +" " +y);

i = sc.nextInt();

j = sc.nextInt();

System.out.println("Enter O");

a[i][j]=sc.next().charAt(0);

n++;

if(n>3){

if(a[0][0]=='O' && a[0][1]== 'O' && a[0][2]=='O'){

System.out.println("Congrats! O won");

exit(0);

}

else if(a[0][0]=='O' && a[1][0]== 'O' && a[2][0]=='O'){

System.out.println("Congrats! O won");

exit(0);

}

else if(a[1][0]=='O' && a[1][1]== 'O' && a[1][2]=='O'){

System.out.println("Congrats! O won");

exit(0);

}

else if(a[0][1]=='O' && a[1][1]== 'O' && a[2][1]=='O'){

System.out.println("Congrats! O won");

exit(0);

}

else if(a[2][0]=='O' && a[2][1]== 'O' && a[2][2]=='O'){

System.out.println("Congrats! O won");

exit(0);

}

else if(a[0][2]=='O' && a[1][2]== 'O' && a[2][2]=='O'){

System.out.println("Congrats! O won");

exit(0);

}

else if(a[0][0]=='O' && a[1][1]== 'O' && a[2][2]=='O'){

System.out.println("Congrats! O won");

exit(0);

}

else if(a[0][2]=='O' && a[1][1]== 'O' && a[2][0]=='O'){

System.out.println("Congrats! O won");

exit(0);

}

}

}

sc.close();

}

private static void exit(int i) {

// TODO Auto-generated method stub

}

}

我已经编写了实现Tic Tac Toe(带有婴儿步骤)的代码。 X或O赢得游戏后,exit(0)实际上并不终止程序执行。 该程序仍在询问下一个输入。 那时候如何终止代码?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值