gpt4 book ai didi

java - 如何使用 JOptionPane 读取文本文件并显示它。 (二维数组)

转载 作者:行者123 更新时间:2023-11-30 09:16:49 24 4
gpt4 key购买 nike

我有 2 个名为 matrix1 和 matrix 2 的文本文件。它们将数字堆叠成 4 行 4 列。如何使用 JOptionPane 读取和显示它?我相信问题出在 showTable 方法上。

public class MainApp {

private static final int ROW = 4;
private static final int COL = 4;

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
double[][] m1 = new double[ROW][COL];
double[][] m2 = new double[ROW][COL];
double[][] m3 = new double[ROW][COL];

double[][] m4 = new double[COL][ROW];


m1 = LeerDatos("matrix1.txt");

showTable("Tabla 1", m1);

m2 = LeerDatos("matrix2.txt");

showTable("Tabla 2", m2);

m3 = sumarDatos(m1, m2);
showTable("m1 + m2: ", m3);
salvarTabla("suma.txt", m3);

m3 = restarTabla(m1,m2);

showTable("m1 - m2: ", m3);
salvarTabla("resta.txt", m3);

String numeroString = JOptionPane.showInputDialog("Escriba el numero escalar: ", "0.0");

double scalar = Double.parseDouble(numeroString);

m3 = scalarMultiplication( scalar, m2);

showTable("multiplicacion escalar con "
+ scalar , m3);
salvarTabla("scalar.txt", m3);

m4 = transpuesta(m1);

showTable("La transpuesta de tabla 1: ", m4);
salvarTabla("transpuesta.txt", m4);

System.exit(0);

}//main

private static void salvarTabla(String string, double[][] m4) {


}

private static double[][] transpuesta(double[][] m1) {


double[][] dummies = new double[m1.length][m1[0].length];
for (int row = 0; row < dummies.length; row++) {
for (int col = 0; col < dummies.length; col++) {

dummies[col][row] = m1[row][col];

}

}
return dummies;
}

private static double[][] scalarMultiplication(double scalar, double[][] m2) {

return null;
}

private static double[][] restarTabla(double[][] m1, double[][] m2) {


double dummies[][] = new double[m1.length][m1[0].length];

for (int row = 0; row < dummies.length; row++) {
for (int col = 0; col < dummies.length; col++) {
dummies[row][col] = m1[row][col] - m2[row][col];

}// for de col

}// for de row


return dummies;
}

private static double[][] sumarDatos(double[][] m1, double[][] m2) {

double dummies[][] = new double[m1.length][m1[0].length];

for (int row = 0; row < dummies.length; row++) {
for (int col = 0; col < dummies.length; col++) {
dummies[row][col] = m1[row][col] + m2[row][col];

}// for de col

}// for de row


return dummies;
}//sumar tabla

private static void showTable(String string, double[][] m1) {
// TODO Auto-generated method stub
double[][] dummies = new double[m1.length][m1[0].length];
for (int row = 0; row < dummies.length; row ++) {
for (int col = 0; col < dummies.length; col++) {

dummies[row][col] = m1[row][col];
JOptionPane.showMessageDialog(null, m1[row][col]);
}

}



}

private static double[][] LeerDatos(String filename) {


File file = new File(filename);


double dummies[][] = new double[ROW][COL];
try {
Scanner scanner = new Scanner ( new File (filename));

for (int row = 0; row < dummies.length; row++) {

for (int col = 0; col < dummies.length; col++) {

dummies[row][col] = scanner.nextDouble();
}

}// inner loop


} catch (FileNotFoundException e) {

e.printStackTrace();


}
return dummies;


}

}

最佳答案

你可以使用这个方法Arrays#deepToString(Object[]) ,并且您避免使用“for 循环”。

JOptionPane.showMessageDialog(null, Arrays.deepToString(m1));

示例:

public static void main(String[] args) {
int [][] numeros = new int[3][2];
JOptionPane.showMessageDialog(null, Arrays.deepToString(numeros));

}

输出:

enter image description here

更新

因为你想做一张 table 之类的东西。您有 2 个选项可以创建自己的 jtable 并将其添加到 JOptionPane 或如本答案中所示 using html

示例 SSCCE:

public class Text{

public static void main(String[] args) {
int[][] numeros = new int[4][4];
int i = 0, j = 0;
StringBuilder sb = new StringBuilder(64);
sb.append("<html><table><tr><td>Col1</td><td>Col2</td><td>Col3</td><td>Col4</td></tr>");
for (i = 0; i < 4; i++) {
sb.append("<tr>");
for (j = 0; j < 4; j++) {
sb.append("<td> ").append(numeros[i][j]).append("</td>");
}
sb.append("</tr>");
}
sb.append("</table></html>");
JOptionPane.showMessageDialog(null, sb);
}


}

输出:

enter image description here

关于java - 如何使用 JOptionPane 读取文本文件并显示它。 (二维数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19280224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com