编写一个名为storeArray的Java程序,询问用户一个正方形数组的大小(相同的行数和列数)。
您的程序应该创建数组,然后用0.0,100.0范围内的随机双精度数填充它
创建数组后,程序必须将数组保存在名为"routes.txt“的文件中,其中第一行是行数,第二行是列数,数组中的数据从第三行开始,直到文件结束。
我的问题是,当我运行程序时,它不打印行数和列数
Random rnd= new Random();
Scanner input= new Scanner(System.in);
System.out.println("enter the number of row");
int rows=input.nextInt();
System.out.println("enter the number of columns");
int columns=input.nextInt();
double [][] array=new double [rows][columns];
PrintWriter outputFile= new PrintWriter ("D:\\routes.txt");
double min=0.0;
double max=100.0;
double maxx= (Math.random() * ((max - min) + 1)) + min;
for (int row=0; row<rows ; row++)
{ System.out.println(array[rows][columns]);
for (int col=0; col<columns; col++)
{
array[row][col]=maxx;
System.out.println(array[row][col]);
outputFile.println(array[row][col]);
}
}
outputFile.close();
}发布于 2019-04-16 13:48:19
像这样修改你的代码
outputFile.println("rows = " + rows);
outputFile.println("columns = " + columns);
for (int row = 0; row < rows; row++) {
for (int col = 0; col < columns; col++) {
array[row][col] = min+(Math.random()*max);
outputFile.println(array[row][col]);
}
}
outputFile.close();首先,您应该删除该行:
Random rnd= new Random();因为你从来没用过它。
另外,改变这一点
double maxx= (Math.random() * ((max - min) + 1)) + min;在我的线路上
array[row][col] = min + (Math.random() * max);将为您的矩阵中的每一行获得一个随机数。
此外,如果您想像这样在main循环中遍历数组
System.out.println(array[rows][columns])您永远不会这样做,因为在每一行上,您仍然没有任何列,因此,您不能这样做,它只能在内部循环中工作,如下所示
System.out.println(array[row][col]);它看起来像你的输出到文件中
https://stackoverflow.com/questions/55701164
复制相似问题