java - Java寻宝游戏
问题描述
我有一个 CA,我必须在其中创建一个寻宝游戏,但我不知道如何将用户输入转换为网格位置,然后在网格中将其与用户输入一起显示。谁能给我一些帮助?我刚开始学习java,所以我是初学者,所以请多多包涵,哈哈。这是我迄今为止尝试使用的代码:
public void printBoard(){
//*this method will actually print the board and fill it with the rows and columns labels
//and will be filled with the users' input: D if dug already or X if treasure been found
board = new int [10][10];
String[] nums = {" 1"," 2"," 3"," 4"," 5"," 6"," 7"," 8"," 9"," 10"};
// char letters = 65;
String[] letters = { " A", " B", " C", " D", " E"," F", " G", " H", " I", " J"};
for (int i = 0 ; i < board.length ; i++){
System.out.print(letters[i]);
// letters++;
}
System.out.println("");
for (int i = 0; i < board.length; i++) {
System.out.print(nums[i]);
for (int j = 0 ; j < board.length ; j++){
if (board[i][j] == 0) { //empty index
System.out.print("__|");
} else if (board[i][j] == 1){ //when user input wont be an empty index so print d|
System.out.println("d|");
} else if(board[i][j] == -1) { //-1 already filled position with the treasure location so print x|
System.out.println("x|");
}
public void getUserInput(){
int userRow, userColumn;
do{
Scanner myScanner = new Scanner(System.in);
System.out.println("Please enter row");
userRow = myScanner.nextInt() -1;
System.out.println("Please enter column");
userColumn = myScanner.nextInt() -1;
if(board[userRow][userColumn] == -1){
System.out.println("That square has been used. Pick again");
}
}while (board[userRow][userColumn] != 0);
if ((board[userRow][userColumn]) == 1){
}
else{
board[userRow][userColumn] = 1;
}
解决方案
推荐阅读
- assembly - x86-64中断期间程序寄存器存储在哪里
- python - 如何复制列表列表并将其附加到 python 中的新列表?
- android - 我该如何解决这个问题?(不能应用运算符!= 'String' to 'Int')
- r - 将图例添加到 pair() 图中
- python-3.x - Python:在 S3 上使用 zeep 和 wsdl
- python - 超时(并跳过)python迭代中的代码块
- java - 更新实体,但未为 @onetomany 关系创建新记录,已存在于 db 表中
- flutter - 为什么我的 Firestore Stream 在快照上的 forEach 上产生重复的结果?
- c++ - 何时调用 C++ 中对象的析构函数?
- javascript - 选择多个 id 并一次传递给控制器