Programming noob here. I'm trying to write a simple program to randomly generate a tic-tac-toe board, display the Xs and Os, and then determine the outcome of the game. My problem is that when I place the String variables in curly brackets in the if/else statements it's telling me they haven't been initialized. Namely, it says variables s11, s13, s21, s22, s31, s32, and s33 haven't been initialized. Yet s12 and s23 are initialized and the only difference I can see is that they aren't in curly brackets. Where have I erred? Thanks.
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); } }
s12ands23chains ofif/elsealways lead to an assignation.b11 != b12then what iss11? etc...