在java中,输入文字,放入一个二维字符数组
In java, input text and put them into a two Dimensional character array
我正在努力了解如何着手解决这个问题。我需要资源来帮助我更好地理解如何正确解决它,或者有人以另一种可能帮助我入门的方式来解释它。或者,如果有人可以给我一个起点或提供与此类似的代码示例。我对此很陌生,尽可能 "broke down" 需要它,这样我才能理解它的所有基本原理。
"Write a program that reads text from a file. Create a 2-dimensional character array that is 6 * 7. Store the characters read in your array in row major order (fill row 0 first, then row 1, etc.). Fill any unused spaces in the 2-D array with the ‘*’ character. If you have more characters than space, ignore the excess characters. Extract the characters from your array in column-major order (pull from column 0 first, then column 1, etc.). Build a new string as you extract the characters. Display the new string."
package programmingExercise5;
import java.util.Scanner;
public class twoDimensionalCharacterArray {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Type in a sentence: ");
String message = scan.nextLine();
}
}
注意:这将用“*”填充字符 space,并用“-”填充 Array 2D 的剩余单元格。
public class twoDimensionalCharacterArray {
public static void main(String[] args) {
int row = 6, col = 7;
char[][] chars = new char[row][col];
Scanner scan = new Scanner(System.in);
System.out.print("Type in a sentence: ");
String message = scan.nextLine();
char[] messages = message.toCharArray();
int i = 0;
for (int r = 0; r < chars.length; r++) {
for (int c = 0; c < col; c++) {
if (i < messages.length) {
chars[r][c] = messages[i] == ' ' ? '*' : messages[i];
i++;
} else {
chars[r][c] = '-';
}
}
}
for (char[] x : chars) {
System.out.println(Arrays.toString(x));
}
}
}
我正在努力了解如何着手解决这个问题。我需要资源来帮助我更好地理解如何正确解决它,或者有人以另一种可能帮助我入门的方式来解释它。或者,如果有人可以给我一个起点或提供与此类似的代码示例。我对此很陌生,尽可能 "broke down" 需要它,这样我才能理解它的所有基本原理。
"Write a program that reads text from a file. Create a 2-dimensional character array that is 6 * 7. Store the characters read in your array in row major order (fill row 0 first, then row 1, etc.). Fill any unused spaces in the 2-D array with the ‘*’ character. If you have more characters than space, ignore the excess characters. Extract the characters from your array in column-major order (pull from column 0 first, then column 1, etc.). Build a new string as you extract the characters. Display the new string."
package programmingExercise5;
import java.util.Scanner;
public class twoDimensionalCharacterArray {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Type in a sentence: ");
String message = scan.nextLine();
}
}
注意:这将用“*”填充字符 space,并用“-”填充 Array 2D 的剩余单元格。
public class twoDimensionalCharacterArray {
public static void main(String[] args) {
int row = 6, col = 7;
char[][] chars = new char[row][col];
Scanner scan = new Scanner(System.in);
System.out.print("Type in a sentence: ");
String message = scan.nextLine();
char[] messages = message.toCharArray();
int i = 0;
for (int r = 0; r < chars.length; r++) {
for (int c = 0; c < col; c++) {
if (i < messages.length) {
chars[r][c] = messages[i] == ' ' ? '*' : messages[i];
i++;
} else {
chars[r][c] = '-';
}
}
}
for (char[] x : chars) {
System.out.println(Arrays.toString(x));
}
}
}