我正在尝试制作一个从输入文本文件中获取二维数组的程序,因此我已经对其进行了修改。我认为它看起来不错,我一直在尝试不同的事情,到目前为止,我遇到的唯一问题是底部的主要方法,并使每种方法的打印功能都可以使用。顺便说一下,我正在Eclipse中进行此操作。出于某种原因,使用列表给我带来了问题,当我在main方法本身中设置了数组时,该程序正在运行,但是我一直在尝试对其进行修改,以使其获取文件。我想提供一种功能,让用户在stratup的命令行上输入文件名,如果没有提供命令行参数,则提示用户输入文件名。所以我认为我走的路正确,但不确定。
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);
}
}
答案 0 :(得分:0)
此方法将打印2个暗淡的数组:
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();
}
}