私の問題の修正を検索しましたが、動作していないようです。 1つの特定の参照番号:thisおよびthis、特にthis。しかし、私はそれらをどのように実装しても、私は理解できないOutOfBoundsErrorを受け取ります。2次元配列の列と行の合計
このプログラムは、クラスの特別な単位です。実際には、それは非常に簡単です -
プログラムの説明:次の問題を解決するには、2次元配列を使用します。会社には、5種類の製品(1〜5)を販売する4人の営業担当者(1〜4人)がいます。 1日に1回、各販売員は、販売された種類の製品ごとに伝票を渡します。各スリップは含まれています
販売者番号
製品番号
その製品の総ドルの値がこのようにその日に
を販売し、各営業担当者は一日あたり0と5の販売伝票の間に渡されます。先月のすべての伝票の情報が利用可能であると仮定します。各データ行には、3つの数字(営業担当者番号、製品番号、売上)が含まれています。先月の売上に関するこのすべての情報を読み、製品ごとの売上高の合計を要約するプログラムを作成します。
提供されたデータ:
1 2 121.77
1 4 253.66
1 5 184.22
1 1 97.55
2 1 152.44
2 2 104.53
2 4 189.97
2 5 247.88
3 5 235.87
3 4 301.33
3 3 122.15
3 2 301.00
3 1 97.55
4 1 125.66
4 2 315.88
4 4 200.10
4 3 231.45
それが列を計算しようとするとエラーにのみ付属しています。私の行は動作します。どのように私はforループまたは配列の行または列のindecesのいずれかを変更しても、それは動作しません。私は最初に私の行を別々に計算してから、私の列の合計を計算しました。私が見落としていることがはっきりと見落とされています。ここで
は私のコードです:
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;
public class prog480u {
static Scanner inFile = null;
public static void main(String[] args) {
try {
// create scanner to read file
inFile = new Scanner(new File ("prog480u.dat"));
} catch (FileNotFoundException e) {
System.out.println("File not found!");
System.exit(0);
}
// make the array
int x = 0;
int y = 0;
double[][] profits = new double[4][5];
while (inFile.hasNext()) {
x = inFile.nextInt(); // use sales numbers as coordinates
y = inFile.nextInt();
profits[x - 1][y - 1] = inFile.nextDouble();
}
// check if it's okay
System.out.println("");
double[][] columnProfits = sums(profits);
for (int a = 0; a < columnProfits.length; a++) {
System.out.print((a+1) + "\t");
for (int b = 0; b < columnProfits[a].length; b++) {
System.out.print(columnProfits[a][b] + "\t");
}
System.out.println("");
}
double[] bottomRow = columnSums(columnProfits);
for (int a = 0; a < bottomRow.length; a++) {
System.out.print("Total:" + bottomRow + "\t");
}
}
public static double[][] sums (double[][] q) {
double[][] array = new double[5][6];
array = q;
double sum = 0;
for (int a = 0; a < array.length; a++) {
for (int b = 0; b < array[0].length; b ++) {
sum += array[a][b]; // add everything in the row
}
array[a][4] = sum; // set that row to the last column
sum = 0; // reset sum to 0
}
return array;
}
public static double[] columnSums (double[][]q) {
double[][] array = new double[5][6];
array = q;
double sum2 = 0;
double[] columns = new double [5];
for (int a = 0; a < array.length; a++) {
for (int b = 0; b < array[0].length; b ++) {
sum2 += array[b][a];
columns[b] = sum2;
}
sum2 = 0; // reset sum to 0
}
return columns;
}
}
はお時間をありがとうございました。私のプログラムは仕事に近づいていると感じていますが、この小さな間違いが私を突っ込んでいます。
あなたは「+ =」を必要とするように見え、ない「=」あなたは、二重 – mcraen