java - 下面的代码从一个文本参数读取并显示一个 mxn 矩阵,我想传递另一个文本参数并将其与第一个 1 进行比较
问题描述
// here is a sample text file below board.txt
/* 4 4
x t x .
. . s .
x . . .
moves.txt lru
l for left, r for right u for up and d for down.
The first text file argument is a board game file on which s represents a start position and t the target.
and the second text file argument is a moves text file. I want to pass the second argument so that I can make those moves across the board file.
*/
public class VGame {
public static void main(String[] args) {
int m = StdIn.readInt();
int n = StdIn.readInt();
String[][] board1 = new String[m][n];
for (int i = 0; i < board1.length; i++) {
// the condition becomes false then gives access back to the outer for loop
for (int j = 0; j < board1[i].length; j++) {
board1[i][j] = StdIn.readString(); //reading from a text files
}
}
// now let's print a two dimensional array in Java for
/*for (char[] a : board1)
{
for (char i : a)
{
System.out.print(i + " ");
} System.out.println("\n");
} */
StdOut.println(m + " " + n);
for (int i = 0; i < board1.length; i++)
{
for (int j = 0; j < board1[i].length; j++)
{
System.out.print(" "+ board1[i][j]);
}
System.out.println();
}
// printing 2D array using Arrays.deepToString() method
//System.out.println("another way to print 2D arrays");
//System.out.println(Arrays.deepToString(board1));
StdOut.println(args[0]);
}
}
解决方案
这应该将索引读取为整数,然后逐行读取字符串。
public class Main {
public static void main(String[] args) {
int m = 0;
int n = 0;
Scanner s = new Scanner(System.in);
System.out.print("input m: ");
m = s.nextInt();
System.out.print("input n: ");
n = s.nextInt();
String[][] board1 = new String[m][n];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print("input string: ");
board1[i][j] = s.nextLine();
}
}
s.close();
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.println(board1[i][j]);
}
}
}
}
推荐阅读
- function - IBM Watson Assistant 调用 IBM Cloud Function - 运行时错误
- php - 如何解决此问题网站在价格和描述选项卡下显示此参数错误?
- intellij-idea - 更新到新的 IntelliJ IDEA 版本后,sbt 任务配置丢失
- raku - 具有可变数量 Real 参数的脚本
- javascript - 如何确保 material-ui 中的整个 Button 组件为 onClick 事件返回相同的 ID 值?
- c++ - 编译时检查 C++ 中结构的更改
- javascript - clean-webpack-plugin 和 file-loader 的 CSS URL 问题
- android - 如何远程在 react-native-push-notifications 中设置自定义声音
- email - 当没有任何工作时,如何从 mac 终端发送邮件?
- plot - 为什么在尝试绘制参数图时,Maxima 在 plot2d 上给我一个错误?