Java井字棋代码编译报‘array required but java.lang.String found’错误求助
Hey there! Let's break down the errors you're seeing and fix your Tic-Tac-Toe code step by step. The core issue here is variable name collisions—you're reusing the same names for your method parameters and your global arrays, which is confusing the compiler big time.
1. Variable Name Conflicts (The Root Cause)
You have two global variables:
String[][] a(the game board array)int[] b(the array tracking used positions)
But your printer method uses the same names for its parameters:
public void printer(int a,String b) { ... }
Inside the method, whenever you try to access a[1][1] or b[0], the compiler thinks you're referring to the method's int a and String b parameters—not your global arrays. That's why you get:
array required but integer foundwhen trying to usea[7][7](it seesaas an int, not an array)array expected,but java.lang.String foundwhen usingb[0](it seesbas a String, not an array)
2. Switch-Case with Strings: Is That Allowed?
Good news—yes, Java 7 and later support String values in switch-case statements, so that part isn't the problem. The issue here is just the variable name conflict messing up the logic inside each case.
3. Other Small Logic Issues
- Inside each case, you're checking
if(b[0]==1)—but that's always checking the first element of the globalbarray (once we fix the name conflict). You should be checking the index corresponding to the position: e.g., for case "a", checkb[0]==1, case "b" checksb[1]==1, etc. - The line
b[0]=(c.compareTo("X")==1)?-1:0;is a bit confusing.c.compareTo("X")will return 1 only ifcis lexicographically greater than "X"—but yourcis either " X " or " O ", so this will always evaluate to 0 (since " O " comes before " X "). You probably want to track which player took the spot, but this logic can be simplified.
Fixed Version of Your Code
Let's rename the method parameters to avoid conflicts, fix the condition checks, and clean up the logic:
// Global variables String[][] gameBoard = {{" "," "," |"," "," "," |"," "," "," ",}, {" "," a "," |"," "," b "," |"," "," c "," ",}, {"___","___","__|","___","___","__|","___","___","___"}, {" "," "," |"," "," "," |"," "," "," ",}, {" "," d "," |"," "," e "," |"," "," f "," ",}, {"___","___","__|","___","___","__|","___","___","___"}, {" "," "," |"," "," "," |"," "," "," ",}, {" "," g "," |"," "," h "," |"," "," i "," ",}, {" "," "," |"," "," "," |"," "," "," ",}}; int[] positionStatus = {1,1,1,1,1,1,1,1,1}; // 1 = available, 0 = X took it, -1 = O took it public void updateBoard(int player, String position) { String marker; if(player == 1) { marker = " X "; } else { marker = " O "; } switch(position) { case "a": if(positionStatus[0] == 1) { gameBoard[1][1] = marker; positionStatus[0] = (player == 1) ? 0 : -1; break; } case "b": if(positionStatus[1] == 1){ gameBoard[1][4] = marker; positionStatus[1] = (player == 1) ? 0 : -1; break; } case "c": if(positionStatus[2] == 1){ gameBoard[1][7] = marker; positionStatus[2] = (player == 1) ? 0 : -1; break; } case "d": if(positionStatus[3] == 1){ gameBoard[4][1] = marker; positionStatus[3] = (player == 1) ? 0 : -1; break; } case "e": if(positionStatus[4] == 1) { gameBoard[4][4] = marker; positionStatus[4] = (player == 1) ? 0 : -1; break; } case "f": if(positionStatus[5] == 1){ gameBoard[4][7] = marker; positionStatus[5] = (player == 1) ? 0 : -1; break; } case "g": if(positionStatus[6] == 1){ gameBoard[7][1] = marker; positionStatus[6] = (player == 1) ? 0 : -1; break; } case "h": if(positionStatus[7] == 1){ gameBoard[7][4] = marker; positionStatus[7] = (player == 1) ? 0 : -1; break; } case "i": if(positionStatus[8] == 1){ gameBoard[7][7] = marker; positionStatus[8] = (player == 1) ? 0 : -1; break; } } }
Key changes made:
- Renamed global arrays to
gameBoardandpositionStatusfor clarity, avoiding conflicts with method parameters - Renamed the method to
updateBoard(more descriptive thanprinter, since it's updating the board, not printing it) - Fixed the condition checks to use the correct index in
positionStatusfor each case - Simplified the logic to set
positionStatusbased directly on theplayerparameter instead of usingcompareTo - Removed the unused
darray from the method
Answers to Your Specific Questions
Can you update a String array's index with a String value in Java?
Absolutely! String arrays store String values, so assigningarray[row][col] = "some string"is totally valid—your problem was just the variable name conflict making the compiler think you weren't referring to the array.Is using a String as a switch-case condition wrong?
Nope, Java 7 and newer versions support String switch cases, so that part was fine. The issue was unrelated to the switch itself.Why did I get those "array required" errors?
As explained earlier, it's because your method parameters had the same names as your global arrays. The compiler prioritizes the local parameters (the ones in the method's parentheses) over global variables, so it thought you were trying to use an int or String as an array.
内容的提问来源于stack exchange,提问作者Shobha singh

