[英]exit(0) is not terminating the program execution in my java code. Why?
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.