[java] 制作tictactoe 一問
public static void main(String[] args) {Scanner input=new Scanner(System.in);
char [] a ={'0','1','2','3','4','5','6','7','8','9'};
int choice;
for (int i=1 ; i<8 ; i++) {
System.out.println(a[1] + " | " + a[2] + " | " +a[3]);
System.out.println(a[4] + " | " + a[5] + " | " +a[6]);
System.out.println(a[7] + " | " + a[8] + " | " +a[9]);
if (a[i]%2==1) {
System.out.print("Player X input the position (1-9) :");
choice=input.nextInt();
a[choice]= 'X'; }
else {
System.out.print("Player O input the position (1-9) :");
choice=input.nextInt();
a[choice]= 'O'; }
}
}
}
显示结果为何会输出两个player o
run:
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
Player X input the position (1-9) :3
1 | 2 | X
4 | 5 | 6
7 | 8 | 9
Player O input the position (1-9) :2
1 | O | X
4 | 5 | 6
7 | 8 | 9
Player O input the position (1-9) : java --------------------编程问答-------------------- 因为你用的是a[i]%2==1,应该是i%2==1
补充:Java , Java SE