Java 如何替换数组的值?
我正在尝试做一个匹配的游戏,我几乎完成了。在给出userInput之后,我试图用隐藏值替换gameboard。我已经写了代码,看看“A1”是否能工作,但它不能。有人知道如何用另一个元素替换数组中的一个元素吗?还是我做错了Java 如何替换数组的值?,java,arrays,matching,Java,Arrays,Matching,我正在尝试做一个匹配的游戏,我几乎完成了。在给出userInput之后,我试图用隐藏值替换gameboard。我已经写了代码,看看“A1”是否能工作,但它不能。有人知道如何用另一个元素替换数组中的一个元素吗?还是我做错了 import java.util.*; public class breitje_MatchUp{ static Scanner input = new Scanner(System.in); static int i=0, count=0; static c
import java.util.*;
public class breitje_MatchUp{
static Scanner input = new Scanner(System.in);
static int i=0, count=0;
static char[][] clearBoard = new char[4][4];
static char[][] board = new char[4][4];
public static void main(String[] args){
gamePlay();
}
public static void drawMenu(){
System.out.println("Welcome to the Match Up game! \n");
System.out.println("Please select from the following: ");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("1. New Game");
System.out.println("2. Quit");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
int optionSelect = input.nextInt();
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
while(optionSelect!=1 && optionSelect !=2){
System.out.println("Please enter 1 (New Game) or 2(Quit)");
optionSelect = input.nextInt();}
if (optionSelect == 1)
System.out.print("START GAME HERE********");
else if (optionSelect == 2)
System.out.println("Thanks for playing! Have a great day.");
}
public static char[][] buildBoard(){
char[][] board = new char[4][4];
for(int i = 0; i <board.length; i ++){
for(int j = 0; j<board[0].length;j++){
board[i][j]=(char)(65+Math.random()*8);
}
}
return board;
}
public static void drawBoard(){
for(int row1 = 0; row1 < 4; row1++) {
// this will create a new line for each new row
System.out.println("");
for(int col1 = 0; col1 < 4; col1++) {
// this will print out the 4 row variable values
clearBoard[row1][col1]='.';
}
}
System.out.format(" %2s %2s %2s %2s \n",'A','B','C','D');
System.out.format(" %2s %2s %2s %2s \n",'-','-','-','-');
System.out.format("1 |%2s %2s %2s %2s \n",
clearBoard[0][0],
clearBoard[0][1],
clearBoard[0][2],
clearBoard[0][3]);
System.out.format("2 |%2s %2s %2s %2s \n",
clearBoard[1][0],
clearBoard[1][1],
clearBoard[1][2],
clearBoard[1][3]);
System.out.format("3 |%2s %2s %2s %2s \n",
clearBoard[2][0],
clearBoard[2][1],
clearBoard[2][2],
clearBoard[2][3]);
System.out.format("4 |%2s %2s %2s %2s \n",
clearBoard[3][0],
clearBoard[3][1],
clearBoard[3][2],
clearBoard[3][3]);
}
public static void gamePlay(){
while(!checkForWin(count)){
drawBoard();
System.out.print("Card 1: Column/Row (Ex: A3) : "); //input 1
String userInput = input.nextLine(); System.out.println();
userInput = userInput.toUpperCase().trim();
convertInput(userInput);
drawBoard();
System.out.print("Card 2: Column/Row (Ex: A3) : "); //input 2
String userInput1 = input.nextLine(); System.out.println();
userInput1 = userInput.toUpperCase().trim();
}
drawMenu();
}
public static void convertInput(String input){
if(input.equals("A1"))
clearBoard[0][0]=board[0][1];
}
public static boolean checkForWin(int count){
if (count == 8)
return true;
else
return false;
}
}
import java.util.*;
公共类breitje_配对{
静态扫描仪输入=新扫描仪(System.in);
静态整数i=0,计数=0;
静态字符[][]clearBoard=新字符[4][4];
静态字符[][]板=新字符[4][4];
公共静态void main(字符串[]args){
游戏性();
}
公共静态菜单(){
System.out.println(“欢迎来到配对游戏!\n”);
System.out.println(“请从以下选项中选择:”);
System.out.println(“~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~”;
System.out.println(“1.新游戏”);
System.out.println(“2.Quit”);
System.out.println(“~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~”;
int options select=input.nextInt();
System.out.println(“~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~”;
while(optionSelect!=1&&optionSelect!=2){
System.out.println(“请输入1(新游戏)或2(退出)”;
选项select=input.nextInt();}
如果(选项选择==1)
System.out.print(“在这里开始游戏*******”;
else if(选项SELECT==2)
System.out.println(“感谢您的参与!祝您度过愉快的一天”);
}
公共静态字符[][]构建板(){
字符[][]板=新字符[4][4];
对于(int i=0;i,您所做的更改将被draw方法覆盖
clearBoard[row1][col1]='.'; // this will print out the 4 row variable values
我不清楚您在这个方法中试图实现什么,因为注释与代码完全不一致-您将值设置为句点,而不是在该行打印任何内容
这里还有很多风格方面需要改进-一个名为draw
的方法不应该有重置电路板的副作用。除了@I82Much的答案,您声明了buildBoard方法,但从未调用过它,所以当您设置clearBoard[0][0]=board[0][1]时,您将其设置为null(我认为)
在buildBoard方法中,您正在声明一个范围为本地的新board数组。请查看JAVA的Arrays API;它没有几个非常有趣和有用的方法。
您希望它做什么,以及它实际做什么?我们需要对这个问题进行更多的澄清。您将元素从一个数组放入另一个数组的代码clearBoard[0][0]=board[0][1]
很好。请注意,在名为board
的类中以及在某些方法中(例如buildBoard
)有一个静态成员您有一个名为board
的局部变量,它将在该方法中对其进行阴影处理。这可能不是一个好主意,而且肯定会让您感到困惑。@RohitJain我希望它将表中的项目替换为匹配游戏的隐藏字母。