Java 在二维阵列中沿对角线移动
我有我的迷宫,可以去东北西南,但我如何移动东北或西南。 如果可能的话,你们能用我的代码给我举个例子吗。谢谢,这是一个4x4阵列 用户界面Java 在二维阵列中沿对角线移动,java,arrays,Java,Arrays,我有我的迷宫,可以去东北西南,但我如何移动东北或西南。 如果可能的话,你们能用我的代码给我举个例子吗。谢谢,这是一个4x4阵列 用户界面 //Begin user dialog System.out.println("Welcome"); input =""; while(!input.equals("quit")) { System.out.println(map.rooms[row][col].name); System.o
//Begin user dialog
System.out.println("Welcome");
input ="";
while(!input.equals("quit"))
{
System.out.println(map.rooms[row][col].name);
System.out.print(">");
input = scan.nextLine().toLowerCase();
switch (input) {
case "n":
if(map.rooms[row][col].isValidExit("n"))
row--;
else
System.out.println("You cant go that way");
break;
case "s":
if(map.rooms[row][col].isValidExit("s"))
row++;
else
System.out.println("You cant go that way");
break;
case "w":
if(map.rooms[row][col].isValidExit("w"))
col--;
else
System.out.println("You cant go that way");
break;
case "e":
if(map.rooms[row][col].isValidExit("e"))
col++;
else
System.out.println("You cant go that way");
break;
您需要对输入进行字符分析
while(!input.equals("quit"))
{
System.out.println(map.rooms[row][col].name);
System.out.print(">");
input = scan.nextLine().toLowerCase();
char[] inputArray = input.toCharArray();
for(char c : inputArray){
switch (input) {
case "n":
if(map.rooms[row][col].isValidExit("n"))
row--;
else
System.out.println("You cant go that way");
break;
case "s":
if(map.rooms[row][col].isValidExit("s"))
row++;
else
System.out.println("You cant go that way");
break;
case "w":
if(map.rooms[row][col].isValidExit("w"))
col--;
else
System.out.println("You cant go that way");
break;
case "e":
if(map.rooms[row][col].isValidExit("e"))
col++;
else
System.out.println("You cant go that way");
break;
东北部示例:
case "ne":
if(map.rooms[row][col].isValidExit("ne")) {
col++;
row--;
}
else {
System.out.println("You cant go that way");
}
break;
抱歉,对于java来说,什么是字符解析。我要去查一下,但一个简单的答案也很有帮助。再简单不过了,我只是在你的代码中添加了两行:但是我的问题是它的意思和作用是什么?从查找它到将字符串转换为字符。我很感谢你给我的代码行,但我想了解和学习,因为我这样做:)。就像我现在把字符放在代码中一样什么?它将字符串转换成字符数组,然后在for循环的帮助下,对数组中包含的每个字母进行大小写转换。因此,您不必添加案例。例如:如果你输入:“seen”,这段代码将向南一次,向东两次,向北一次。啊,我看到了,哇,这是一个有用的工具。谢谢佩德里托和凯。我唯一的问题是这是我的项目,我们还没有真正涉及到char,所以不确定他是否会期待这一点。不管是哪种方式,谢谢我会继续四处寻找并做trail n error你需要在if:case“ne”后面加括号:if(map.rooms[row][col.isValidExit(“ne”){col++;row--;}else System.out.println(“你不能走那条路”);打破