英文:
Java treasure hunt game
问题
我在一个CA(课程作业)中需要创建一个寻宝游戏,但我不知道如何将用户输入转换为网格位置,然后在网格中显示带有用户输入的内容。有人可以给我一些帮助吗?我刚开始学习 Java,所以我是一个非常初学者,请耐心一点哈。以下是我到目前为止尝试使用的代码:
public void printBoard(){
//*此方法将实际打印出带有行列标签的棋盘,
//并将使用者的输入填入其中:如果已经挖过,则填入 D,如果找到了宝藏,则填入 X
board = new int[10][10];
String[] nums = {" 1"," 2"," 3"," 4"," 5"," 6"," 7"," 8"," 9"," 10"};
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]);
}
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) {
System.out.print("__|");
} else if (board[i][j] == 1){
System.out.println("d|");
} else if (board[i][j] == -1) {
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;
}
}
请注意,以上是您提供的代码的翻译。如果您有任何问题或需要进一步的帮助,请随时问我。
英文:
I got a CA where i have to create a treasure hunt game and I cant figure out how to take the user input convert it to grid location and then display it with the user input in the grid. Can anyone give me some help? I just started learning java so i am very beginner so please bear with me lol. this is the code i been trying to use so far:
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;
}
专注分享java语言的经验与见解,让所有开发者获益!
评论