java在2d数组上得到一个空输出不确定哪里出错了
我在编程课上遇到了问题,我被困在了一个我不确定该去哪里解决的问题上
问题是:
世界级将包含三个私有数据字段。第一个私有数据字段是2 代表世界的多维字符数组。世界的大小将取决于世界的大小 传递给构造函数。下一个私有数据字段是characterRow,最后一个是private 数据字段为characterColumn。 构造函数将接受两个值:宽度和高度。使用宽度和高度设置大小 二维数组的。然后用破折号填充数组。角色行和 characterColumn数据字段都设置为0,然后将字符“P”放置在位置 characterRow和characterColumn。 所以,如果你的世界是打印宽度9,或9列,高度4,或4行,它会 比如在创造之后:
P--------
---------
---------
---------
世界级最初将包含5个公共方法。这些方法是向上移动的, moveDown、moveLeft、moveRight和displayWorld
我得到了以下信息:
Enter number of rows:
9
Enter number of columns:
9
P - null - null - null - null - null - null - null - null -
null - null - null - null - null - null - null - null - null -
null - null - null - null - null - null - null - null - null -
null - null - null - null - null - null - null - null - null -
null - null - null - null - null - null - null - null - null -
null - null - null - null - null - null - null - null - null -
null - null - null - null - null - null - null - null - null -
null - null - null - null - null - null - null - null - null -
null - null - null - null - null - null - null - null - null -
- - - - - - - - -
- - - - - - - - -
- - - - - - - - -
- - - - - - - - -
- - - - - - - - -
- - - - - - - - -
- - - - - - - - -
- - - - - - - - -
- - - - - - - - -
Commands can be UP, DOWN, LEFT, RIGHT, or EXIT. EXIT closes the program.
Enter command:
我可以得到这样的结果:用户输入数组的大小,但是数组在正确的位置打印空字符,然后再次正确打印,没有字符。我不确定我在这里偏离了轨道。你能帮我看看吗?下面是我为这两个类编写的代码
驾驶员等级:
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
World world = new World();
System.out.println("Commands can be UP, DOWN, LEFT, RIGHT, or EXIT. EXIT closes the program.");
System.out.println("Enter command: ");
Scanner input = new Scanner(System.in);
while (true)
{
String s = input.nextLine();
switch (s)
{
case "up":
world.moveUp();
world.displayWorld();
break;
case "down":
world.moveDown();
world.displayWorld();
break;
case "left":
world.moveLeft();
world.displayWorld();
break;
case "right":
world.moveRight();
world.displayWorld();
break;
case "exit":
break;
}
}
}
}
世界级:
import java.util.*;
public class World
{
private static final String P = "P";
private String[][] array;
public World()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter number of rows: ");
int crow = input.nextInt();
System.out.println("Enter number of columns: ");
int ccol = input.nextInt();
array = new String[crow][ccol];
array[0][0]=P;
displayWorld();
for(int i = 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
{
array[i][j] = new String();
}
}
displayWorld();
}
public void displayWorld()
{
System.out.println();
for(int i = 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
{
System.out.print(array[i][j] + " - ");
}
System.out.println();
}
}
public void moveUp()
{
for(int i= 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
{
if ((array[i][j]) == " - ")
{
if (i < array.length - 1)
{
array[i][j] = " - ";
array[i - 1][j] = P;
}
return;
}
}
}
}
public void moveDown()
{
for(int i= 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
{
if ((array[i][j]) == " - ")
{
if (i < array.length - 1)
{
array[i][j] = " - ";
array[i + 1][j] = P;
}
return;
}
}
}
}
public void moveLeft()
{
for(int i= 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
{
if ((array[i][j]) == " - ")
{
if (i < array.length - 1)
{
array[i][j] = " - ";
array[i][j - 1] = P;
}
return;
}
}
}
}
public void moveRight()
{
for(int i= 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
{
if ((array[i][j]) == " - ")
{
if (i < array.length - 1)
{
array[i][j] = " - ";
array[i][j + 1] = P;
}
return;
}
}
}
}
}
# 1 楼答案
在构造函数中,在填充数组之前有一个displayWorld方法调用。然后,当你填充它时,你重新填充你的位置[0][0],P消失。通过使用上面的代码,你会得到你想要的
我希望我帮助过你