java - 我的二维数组程序的打印输出出现问题
问题描述
我正在尝试制作一个从输入文本文件中获取二维数组的程序,所以我已经对其进行了修改。我认为它看起来不错,我一直在尝试不同的东西,到目前为止我遇到的唯一问题是底部的主要方法以及让打印功能适用于每种方法。顺便说一句,我在 Eclipse 中这样做。由于某种原因,使用列表给我带来了问题,当我在 main 方法本身中设置了一个数组时,程序正在运行,但我一直在尝试修改它以便它需要一个文件。我想提供让用户在命令行中输入文件名的功能,如果没有提供命令行参数,则提示用户输入文件名。所以我认为我在正确的轨道上,但不确定。
import java.io.*;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class TwoDimArray
{
private static int[][] twodim;
public TwoDimArray() {
loadArray();
}
//main function
public static int getTotal(int [][]numbers)
{
int tot=0;
for(int row=0;row<numbers.length;row++)
for(int col=0;col<numbers[row].length;col++)
tot+=numbers[row][col];
return tot;
}
public static double getAverage(int [][]numbers)
{
double avg;
avg= (double)(getTotal(numbers)/(12));
return avg;
}
public static int getRowTotal(int [][]numbers, int index)
{
int tot=0;
for(int col=0;col<4;col++)
tot+=numbers[index][col];
return tot;
}
public static int getColumnTotal (int [][]numbers, int index)
{
int tot=0;
for(int row=0;row<numbers.length;row++)
tot+=numbers[row][index];
return tot;
}
public static int getHighest(int [][]numbers,int row)
{
int high=numbers[row][0];
for(int i=1;i<numbers[row].length;i++)
if(numbers[row][i]>high)
high=numbers[row][i];
return high;
}
public static int getLowest(int [][]numbers,int row)
{
int low=numbers[row][0];
for(int i=1;i<numbers[row].length;i++)
if(numbers[row][i]<low)
low=numbers[row][i];
return low;
}
public void loadArray() {
String fileName = "";
try {
fileName = JOptionPane.showInputDialog("Enter an input file name: ");
if (fileName == null)
throw new NullPointerException("Please do not press cancel!");
Scanner infile = new Scanner(new File("input.txt"));
if (!infile.hasNextLine()) {
JOptionPane.showMessageDialog(null, "The input file " + fileName + " is empty!");
System.exit(1);
}else {
infile.reset();
String strRows = infile.next();
String strCols = infile.next();
int rows = Integer.parseInt(strRows);
int cols = Integer.parseInt(strCols);
twodim = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
String data = infile.next();
twodim[i][j] = Integer.parseInt(data);
}
}
infile.close();
}
} catch (NullPointerException npe) {
JOptionPane.showMessageDialog(null, npe.getMessage());
System.exit(1);
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "cannot convert input to integer");
System.exit(1);
}catch (FileNotFoundException fnfe) {
JOptionPane.showMessageDialog(null, "The input file " + fileName + " doesn't exist!");
System.exit(1);
}
}
public static void main (String []arg)
{
//private int[][] twodim;
//int [][]list= {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
//Function call and display data returned
TwoDimArray tda = new TwoDimArray();
System.out.println("Total:"+getTotal(list));
System.out.println("Average:"+getAverage(list));
System.out.println("Row 2 value:"+getRowTotal(list,2));
System.out.println("Column 3 Total :"+getColumnTotal(list,3));
System.out.println("Highest value in row 1 is :"+getHighest(list,1));
System.out.println("Lowest value in row 2 is :"+getLowest(list,2));
//Exit program
System.exit(0);
}
}
解决方案
This method prints a 2 dim array:
public void printMatrix(int[][] matrix) {
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[0].length; col++) {
System.out.print(matrix[row][col] + "\t");
}
System.out.println();
}
}
推荐阅读
- c# - 在 C# 中的一个线程上读取流并在另一个线程上写入流
- node.js - 当我在 React 中开发前端时,我应该考虑/考虑/管理 Node 服务器中的内存消耗吗?
- python - python lambda根据现有列增加值
- java - 具有 Java BigDecimal 类型的 Avro
- python - 具有混合输入数据的功能 API。得到这个错误: AttributeError: 'tuple' object has no attribute 'ndim'
- amazon-web-services - returning JSON response from AWS Glue Pythonshell job to the boto3 caller
- python - 如何在读取两个输入文件时多次运行python脚本
- android - Google Play 64 位要求 App bundle 更新失败
- node.js - Nodejs 的授权 SSL
- python - 为什么我的 excel 文件在被 Panda's 更新时会跳到 0 Kb?