gpt4 book ai didi

java - exit(0) 没有终止我的java代码中的程序执行。为什么?

转载 作者:行者123 更新时间:2023-12-01 07:49:55 25 4
gpt4 key购买 nike

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) 实际上并不会终止程序执行。该程序仍在请求下一个输入。如何在那一刻终止代码?

最佳答案

您的 exit(0) 正在调用一个空方法。替换为:

private static void exit(int i) {
System.exit(i);
}

关于java - exit(0) 没有终止我的java代码中的程序执行。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39952261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com