如何将矩阵中的元素设置为空白“”,而不是默认值 "null"
How to set elements in a matrix to blank " ", instead of default "null"
我有以下代码,我希望用户输入存储,然后代码循环返回下一个输入存储,直到他们点击“Q”,然后退出。我不知道该怎么做。
另外,我希望在用户设置大小后,我的 2d 数组打印为空白,而不是默认的 0。所以如果用户说他们想输入 SIZE = 4x4 "row =1, column =2, input =7" 它会打印 "These ZEROS would be BLANK
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,但这会导致问题,它不会在循环后保存用户输入。
您的 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
,而不是创造他们自己的操纵,后者随着他们的 return 而死。在研究循环用户输入并与数组交互之前,您需要解决所有这些问题。
关于 0 的打印,这是上述问题之一的副作用。在 printMatrix 中,您声明一个新的 int[][] newArray
并打印它。 int 的默认值是 0,所以你得到全 0。如果您使用的是 String 数组,则每一行都会得到所有“nullnullnull..”,因为 String 的默认值为 null。如果您最初想要所有空白,则必须在构造函数中将数组初始化为所有空字符串,或者在循环打印 space 数组时处理 null。
在循环用户输入时,您还需要在 while 之前在循环内再次请求它,否则用户输入只会被请求一次,您将永远循环使用该选项。
祝你好运,这似乎是一个让你自己熟悉 OO 和数组操作的很好的练习,但你代码中的很多问题都超出了单个 SO 答案的范围,而且有点像课堂作业。我已经修复了您的示例代码并根据您的需要进行操作,只需进行少量更改,所以您很接近了。
编辑:
将来,请不要不断编辑问题以反映进一步的发展,除非您将原始问题留在原处并添加到它并且它仍然反映原始问题的延续,因为它使答案和评论没有上下文。 SO 更像是针对所面临的特定问题的问答,而不是针对持续开发提供反馈的论坛。如果您通过答案解决了特定问题,请将该答案标记为解决方案并将任何新问题移至新问题,否则您可能会面临关闭问题的风险。请参阅 https://whosebug.com/help/asking, especially https://whosebug.com/help/how-to-ask 下的前几个主题,了解我的意思。我很温柔,但这个社区不能总是这样。
话虽这么说,但由于您似乎在这里做出了真正的努力,因此这里有一些基于您的评论和编辑的建议。
如果要用 space 预填充行,
for (String[] row : newArray)
Arrays.fill(row, " ");
代码块不会打印,因为每次调用打印时都会将其清空。它应该进入您的构造函数,以便它只在创建对象时发生一次。
或者,如果你想在打印方法中处理它,比如
System.out.print(newArray[i][j] == null ? " " : newArray[i][j]);
会成功,使用三元运算符在遇到时打印出“”而不是 null。
您也不需要在这些方法中更新扫描仪,但这不会影响功能。
我有以下代码,我希望用户输入存储,然后代码循环返回下一个输入存储,直到他们点击“Q”,然后退出。我不知道该怎么做。 另外,我希望在用户设置大小后,我的 2d 数组打印为空白,而不是默认的 0。所以如果用户说他们想输入 SIZE = 4x4 "row =1, column =2, input =7" 它会打印 "These ZEROS would be BLANK
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,但这会导致问题,它不会在循环后保存用户输入。
您的 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
,而不是创造他们自己的操纵,后者随着他们的 return 而死。在研究循环用户输入并与数组交互之前,您需要解决所有这些问题。
关于 0 的打印,这是上述问题之一的副作用。在 printMatrix 中,您声明一个新的 int[][] newArray
并打印它。 int 的默认值是 0,所以你得到全 0。如果您使用的是 String 数组,则每一行都会得到所有“nullnullnull..”,因为 String 的默认值为 null。如果您最初想要所有空白,则必须在构造函数中将数组初始化为所有空字符串,或者在循环打印 space 数组时处理 null。
在循环用户输入时,您还需要在 while 之前在循环内再次请求它,否则用户输入只会被请求一次,您将永远循环使用该选项。
祝你好运,这似乎是一个让你自己熟悉 OO 和数组操作的很好的练习,但你代码中的很多问题都超出了单个 SO 答案的范围,而且有点像课堂作业。我已经修复了您的示例代码并根据您的需要进行操作,只需进行少量更改,所以您很接近了。
编辑: 将来,请不要不断编辑问题以反映进一步的发展,除非您将原始问题留在原处并添加到它并且它仍然反映原始问题的延续,因为它使答案和评论没有上下文。 SO 更像是针对所面临的特定问题的问答,而不是针对持续开发提供反馈的论坛。如果您通过答案解决了特定问题,请将该答案标记为解决方案并将任何新问题移至新问题,否则您可能会面临关闭问题的风险。请参阅 https://whosebug.com/help/asking, especially https://whosebug.com/help/how-to-ask 下的前几个主题,了解我的意思。我很温柔,但这个社区不能总是这样。
话虽这么说,但由于您似乎在这里做出了真正的努力,因此这里有一些基于您的评论和编辑的建议。
如果要用 space 预填充行,
for (String[] row : newArray)
Arrays.fill(row, " ");
代码块不会打印,因为每次调用打印时都会将其清空。它应该进入您的构造函数,以便它只在创建对象时发生一次。
或者,如果你想在打印方法中处理它,比如
System.out.print(newArray[i][j] == null ? " " : newArray[i][j]);
会成功,使用三元运算符在遇到时打印出“”而不是 null。
您也不需要在这些方法中更新扫描仪,但这不会影响功能。