在这里编程菜鸟。我正在尝试编写一个简单的程序来随机生成一个井字棋盘,显示 Xs 和 Os,然后确定游戏的结果。我的问题是,当我将字符串变量放在 if/else 语句中的大括号中时,它告诉我它们还没有被初始化。即,它表示变量 s11、s13、s21、s22、s31、s32 和 s33 尚未初始化。然而 s12 和 s23 已初始化,我能看到的唯一区别是它们不在大括号中。我哪里错了?谢谢。
import java.util.Random;
public class Lab7 {
public static void main(String[] args) {
int b11, b12, b13, b21, b22, b23, b31, b32, b33;
int x, o;
String s11, s12, s13, s21, s22, s23, s31, s32, s33;
x = 0;
o = 0;
Random r = new Random();
b11 = r.nextInt(3);
b12 = r.nextInt(3);
b13 = r.nextInt(3);
b21 = r.nextInt(3);
b22 = r.nextInt(3);
b23 = r.nextInt(3);
b31 = r.nextInt(3);
b32 = r.nextInt(3);
b33 = r.nextInt(3);
if ((b11 == b12) && (b12 == b13))
if (b11 == 0)
{
o = o+1;
s11 = "O";
}
else if (b11 == 1)
{
x = x+1;
s11 = "X";
}
else
s11 = "";
if ((b21 == b22) && (b22 == b23))
if (b21 == 0)
{
o = o+1;
s21 = "O";
}
else if (b21 == 1)
{
x = x+1;
s21 = "X";
}
else
s21 = "";
if ((b31 == b32) && (b32 == b33))
if (b31 == 0)
{
o = o+1;
s31 = "O";
}
else if (b31 == 1)
{
x = x+1;
s31 = "X";
}
else
s31 = "";
if ((b11 == b21) && (b21 == b31))
if (b11 == 0)
o = o+1;
else if (b11 == 1)
x = x+1;
if ((b32 == b22) && (b22 == b12))
if (b32 == 0)
{
o = o+1;
s32 = "O";
}
else if (b32 == 1)
{
x = x+1;
s32 = "X";
}
else
s32 = "";
if ((b33 == b23) && (b23 == b13))
if (b33 == 0)
{
o = o+1;
s33 = "O";
}
else if (b33 == 1)
{
x = x+1;
s33 = "X";
}
else
s33 = "";
if ((b22 == b11) && (b22 == b33))
if (b22 == 0)
{
o = o+1;
s22 = "O";
}
else if (b22 == 1)
{
x = x+1;
s22 = "X";
}
else
s22 = "";
if ((b13 == b22) && (b22 == b31))
if (b13 == 0)
{
o = o+1;
s13 = "O";
}
else if (b13 == 1)
{
x = x+1;
s13 = "X";
}
else
s13 = "";
if (b12 == 0)
s12 = "O";
else if (b12 == 1)
s12 = "X";
else
s12 = "";
if (b23 == 0)
s23 = "O";
else if (b23 == 1)
s23 = "X";
else
s23 = "";
System.out.println(s11+" "+s12+" "+s13);
System.out.println(s21+" "+s22+" "+s23);
System.out.println(s31+" "+s32+" "+s33);
}
}