如何将矩阵中的元素设置为空白“”,而不是默认的“null”

问题描述 投票:0回答:1

我有以下代码,我希望用户输入进行存储,然后代码循环返回并存储下一个输入,直到他们点击“Q”,然后退出。我不知道该怎么做。 另外,我希望在用户设置大小后将我的二维数组打印为空白,而不是默认的 0。因此,如果用户说他们想要输入 SIZE = 4x4“行=1,列=2,输入=7”,它将打印“这些零将是空白

0000
0070
0000
0000

输入“第2行,第1列,输入A,它将打印

0000
0070
0700
0000

到目前为止我的代码

import java.util.*;

public class MainProg {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("How many rows do you want for your matrix?  ");
        int row = in.nextInt();

        System.out.print("How many columns do you want for your matrix?  ");
        int column = in.nextInt();
        String[][] newArray = new String[row][column];


        Array2 twoDArray = new Array2(row, column, newArray); //calling my class


        do {
            System.out.println("If you would like to set an, element press S: " + "\n" +
                    "If you would like to set an element, press G" + "\n" +
                    "If you would like to empty an element, press E" + "\n" +
                    "If you would like to print an element, press P" + "\n" +
                    "If you would like to quit, press Q");
            String userInput = in.next();


            if (userInput.equalsIgnoreCase("S")) {
                twoDArray.setElement();
            } else if (userInput.equalsIgnoreCase("G")) {
                twoDArray.getElement();
            } else if (userInput.equalsIgnoreCase("E")) {
                twoDArray.clearElement();
            } else if (userInput.equalsIgnoreCase("P")) {
                twoDArray.printMatrix();
                //you will do you toString here
            } else if (userInput.equalsIgnoreCase("Q")) {
                //this will quit the program
                twoDArray.quitProgram();
            }
            //break;
        } while (true);
    }
}
import java.util.*;

public class Array2 {
    MainProg main1 = new MainProg();
    Scanner in = new Scanner(System.in);
    private String [][] newArray;
    private int row;
    private int column;



    public Array2(int row, int column, String[][] newArray) {
        this.row = row;
        this.column = column;
        this.newArray = newArray;
    }

    public void getElement() {
        Scanner in = new Scanner(System.in);
        System.out.println("What row is the element you would like to get in? (Must be under " + row + ")");
        int userRow = in.nextInt();
        System.out.println("What column is the element you like to get in? (Must be under " + column + ")");
        int userCol = in.nextInt();
        System.out.println("You have entered: " + "\n" +
                "Row " + userRow + "\n" +
                "Column " + userCol);
        String getElement = newArray[userRow-1][userCol-1];
        System.out.println(getElement);
    }

    public void setElement() {
        System.out.println("What row would you like your element in? (Must be under " + row + ")");
        int userRow = in.nextInt();
        System.out.println("What column would you like your element in? (Must be under " + column + ")");
        int userCol = in.nextInt();
        System.out.println("What character would you like the element to be?");
        String userChar = in.next();
        System.out.println("You have entered: " + "\n" +
                "Row " + userRow + "\n" +
                "Column " + userCol + "\n" +
                "Char to be entered: " + userChar);
        newArray[userRow][userCol] = String.valueOf(userChar);
    }

    public void clearElement() {
        Scanner in = new Scanner(System.in);
        System.out.println("What row is the element you would like empty? (Must be under " + row + ")");
        int userRow = in.nextInt();
        System.out.println("What column is the element you like to empty? (Must be under " + column + ")");
        int userCol = in.nextInt();
        System.out.println("You have entered: " + "\n" +
                "Row " + userRow + "\n" +
                "Column " + userCol);

    }

    public void printMatrix() {
        Scanner in = new Scanner(System.in);
        //String result = " ";
        System.out.println("The array is: \n");
        for (int i = 0; i < newArray.length; i++) {
            for (int j = 0; j < newArray[i].length; j++) {
                System.out.print(newArray[i][j]);
            }
            //for (String[] row: newArray) THIS LEAD TO A PROBLEM
            //    Arrays.fill(row, " ");
            System.out.println();
        }
    }

    public void quitProgram() {
        System.out.println("The system will now exit! BYE!!!");
        System.exit(0);
    }
}

我编辑了我的代码,以便回答一些问题。现在我剩下的唯一问题是让我的矩阵最初填充空白“”,而不是默认的“null”。我尝试在 printMatrix 方法中使用 Arrays.fill,但这会导致问题,它不会在循环后保存用户输入。

java arrays multidimensional-array user-input
1个回答
2
投票

您的 IndexOutOfBoundsException (现已编辑)是由于您在 setElement 中使用

row
column
而不是
userRow
userColumn
,这是您存储用户输入的位置。
row
column
将引用类成员变量,它们都是 5,因为您将其设置为 5x5 矩阵,因此高于最大索引 4。您还需要将 char 转换为字符串,因为您的使用
String[][]

System.out.println("You have entered: " + "\n" +
        "Row " + userRow + "\n" +
        "Column " + userCol + "\n" +
        "Char to be entered: " + userChar);
newArray[row][column] = userChar;

最后一行应该是

newArray[userRow][userColumn] = String.valueOf(userChar);
。尽管您可能想检查这些值是否小于
row
column
以避免更多此类异常。

即使修复了该问题,您的代码当前仍存在其他问题。最大的问题是,您当前在大多数方法中定义和使用新数组,而不是成员变量 newArray,因此对 get/set/clear/print 的方法调用并没有像您期望的那样使用您的实例,而是使用新的空数组每一次。这些应该操纵

this.newArray
,而不是创造自己的操纵,随着他们的回归而消亡。在考虑循环用户输入并与数组交互之前,您需要解决所有问题。

关于 0 的打印,这是上述问题之一的副作用。在 printMatrix 中,您声明一个新的

int[][] newArray
并打印它。 int 的默认值是 0,所以你得到的都是 0。如果您使用 String 数组,您将获得每行的所有“nullnullnull..”,因为 String 的默认值为 null。如果您最初想要所有空白,则必须在构造函数中将数组初始化为所有空字符串,或者在循环遍历数组时打印空格来处理 null。

在循环获取用户输入时,您还需要在 while 之前在循环内再次请求它,否则只会请求一次用户输入,并且您将永远使用该选项循环。

祝你好运,这似乎是熟悉 OO 和数组操作的一个很好的练习,但是代码中的很多问题超出了单个 SO 答案的范围,并且有点像课堂作业。我已经修复了您的示例代码,并根据您的需要进行了操作,只需进行少量更改,所以您已经很接近了。

根据您的评论和编辑,这里还有一些建议。

如果您想在行中预先填充空格,则

for (String[] row : newArray)
    Arrays.fill(row, " ");

代码块不会被打印,因为每次调用 print 时都会将其清空。它应该进入你的构造函数,这样它只在创建对象时发生一次。

或者,如果您想在打印方法中处理它,类似

System.out.print(newArray[i][j] == null ? " " : newArray[i][j]);

可以解决这个问题,在遇到时使用三元运算符打印出“”而不是 null。

您也不需要在这些方法中更新扫描仪,但这不会影响功能。

© www.soinside.com 2019 - 2024. All rights reserved.