如何解决尝试将用户选择的单元格与相邻的单元格交换
所以这里的目标是尝试创建一个三消风格的游戏。现在我试图让玩家选择一个单元格并将其与具有特定方向的相邻单元格交换。但是我在注册输入时遇到问题。由于某种原因,它不会接受我对该方向的任何输入为有效。无论出于何种原因,它还会请求单元格和行 3 次。
import java.util.*;
public class Match3{
private int rowsN;
private int columnsM;
private int p;
private int[][] matrixArr = new int[rowsN][columnsM];
Scanner scan = new Scanner(System.in);
public Match3(int n,int m){
rowsN = n;
columnsM = m;
matrixArr = new int[n][m];
}
public void printMatrix(){
for(int i = 0; i < matrixArr.length; i++){
for(int j = 0; j < matrixArr.length; j++){
System.out.print("[" + matrixArr[i][j] + "] ");
}
System.out.println();
}
System.out.println();
}
public void ConstructMatrix(){
for(int i = 0; i < matrixArr.length; i++){
for(int j = 0; j < matrixArr.length; j++){
Random rng = new Random();
matrixArr[i][j] = rng.nextInt(p) + 1;
System.out.print("[" + matrixArr[i][j] + "] ");
}
System.out.println();
}
System.out.println();
}
public void setDiff(){
System.out.println("Please enter the difficulty: ");
int diff = scan.nextInt();
p = diff;
System.out.print("The difficulty you selected is " + getDiff() + ".");
System.out.println();
}
public int getDiff(){
return p;
}
public void swapNums(){
int x;
int y;
String dir;
int temp;
System.out.println("Please select the row of cell you wish to move: ");
while(scan.nextInt() > matrixArr.length) System.out.println("Please enter a valid position");
if(scan.nextInt() <= matrixArr.length){
y = scan.nextInt();
System.out.println("Please select the column of cell you wish to move: ");
while(scan.nextInt() > matrixArr.length) System.out.println("Please enter a valid position");
if(scan.nextInt() <= matrixArr.length){
x = scan.nextInt();
System.out.println("Please select the direction of the cell you wish to swap with (u,l,r,d): ");
while(scan.next() != "u" || scan.next() != "l" || scan.next() != "r" || scan.next() != "d") System.out.println("Please enter a valid direction");
if(scan.next() == "u" || scan.next() == "l" || scan.next() == "r" || scan.next() == "d"){
dir = scan.next();
for(int i = 0; i < matrixArr.length; i++){
for(int j = 0; j < matrixArr.length; j++){
if(i == x && j == y){
temp = matrixArr[i][j];
if(dir == "u") {
matrixArr[i][j] = matrixArr[i][j - 1];
matrixArr[i][j - 1] = temp;
printMatrix();
}
else{
if(dir == "l") {
matrixArr[i][j] = matrixArr[i - 1][j];
matrixArr[i - 1][j] = temp;
printMatrix();
}
else{
if(dir == "r") {
matrixArr[i][j] = matrixArr[i + 1][j];
matrixArr[i + 1][j] = temp;
printMatrix();
}
else{
if(dir == "d") {
matrixArr[i][j] = matrixArr[i][j + 1];
matrixArr[i][j + 1] = temp;
printMatrix();
}
}
}
}
}
}
}
}
}
}
}
public void runProgram(){
System.out.println("This game has " + rowsN + " rows and " + columnsM + " columns.");
System.out.println();
setDiff();
System.out.println();
ConstructMatrix();
swapNums();
}
public static void main(String[] args){
Match3 game1 = new Match3(3,3);
game1.runProgram();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。