我有一个二维数组程序,我遇到了问题。我必须在程序中读取存有数字的文件。问题是,有10名员工,我需要在周日至周六的每周工作时间。
文件(prog 2.dat)是:
10
8 4 7 3 8 6 3
2 7 6 3 5 2 1
1 2 3 8 6 4 4
3 2 8 8 8 5 1
4 3 2 1 3 8 6
8 5 6 7 5 5 4
1 8 7 4 2 8 6
1 5 4 6 5 3 3
4 3 2 1 2 3 4
1 8 7 6 5 6 5
我的节目是:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class program2 {
public static void main(String[] args) {
File hours = new File("prog2.dat"); // read file
Scanner fileIn = null;
int[][] array = new int[10][7];
int[] total = new int[array.length];
int[] finalTotal = new int[array.length];
int[] employees = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
try {
fileIn = new Scanner(hours);
} catch (FileNotFoundException e) {
while (fileIn.hasNext()) {
for (int i = 0; i < 10; i++) {
for (int a = 0; a < 7; a++) {
int num = fileIn.nextInt();
array[i][a] = num;
}
}
}
// takes employees hour total
for (int i = 0; i < 10; i++) {
total[i] = array[i][0] + array[i][1] + array[i][2] + array[i][3] + array[i][4]
+ array[i][5] + array[i][6];
}
// takes the hours and sorts from greatest to least
for (int i = 0; i < 7; i++) {
int greatest = total[i];
for (int b = i + 1; b < 10; b++) {
if (total[b] > greatest) {
int employeeTemp = employees[i];
employees[i] = employees[b];
employees[b] = employeeTemp;
int tempNum = greatest;
greatest = total[b];
total[i] = greatest;
total[b] = tempNum;
}
}
}
}
// print employee number and worked hours
for (int i = 0; i < 10; i++) {
System.out.println(" Employee #" + employees[i] + ": " + total[i]);
}
}
}
我想让一名员工#:然后他们的工作时间有效,但所有员工都打印出零。
为什么所有员工的打印都为零?
答案 0 :(得分:1)
你可能有不好的缩进:逻辑都在捕获中,这肯定不是你想要的。
写入全部为零,因为事实上,流程的执行会直接将系统带到系统中而不会被利用:没有异常
最后移动捕获,你会得到
员工#5:40
员工#0:39
员工#9:38
员工#6:36
员工#3:35
员工#2:28
员工#7:27
员工#1:26
员工#8:19
员工#4:27
public static void main(String[] args) {
File hours = new File("prog2.dat"); //read file
Scanner fileIn = null;
int[][] array = new int[10][7];
int[] total = new int[array.length];
int[] finalTotal = new int[array.length];
int[] employees = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
try {
fileIn = new Scanner(hours);
while (fileIn.hasNext()) {
for (int i = 0; i < 10; i++) {
for (int a = 0; a < 7; a++) {
int num = fileIn.nextInt();
array[i][a] = num;
}
}
}
// takes employees hour total
for (int i = 0; i < 10; i++) {
total[i] = array[i][0] + array[i][1] + array[i][2] +
array[i][3] + array[i][4] + array[i][5] + array[i][6];
}
// takes the hours and sorts from greatest to least
for (int i = 0; i < 7; i++) {
int greatest = total[i];
for (int b = i + 1; b < 10; b++) {
if (total[b] > greatest) {
int employeeTemp = employees[i];
employees[i] = employees[b];
employees[b] = employeeTemp;
int tempNum = greatest;
greatest = total[b];
total[i] = greatest;
total[b] = tempNum;
}
}
}
// print employee number and worked hours
for (int i = 0; i < 10; i++) {
System.out.println(" Employee #" + employees[i] + ": " +
total[i]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
答案 1 :(得分:1)
此代码正常运行
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class program2 {
public static void main(String[] args) {
File hours = new File("C:\\prog2.dat"); // Give your file path
Scanner fileIn = null;
int[][] array = new int[10][7];
int[] total = new int[array.length];
int[] finalTotal = new int[array.length];
int[] employees = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int numberOfEmployees = 0;
try {
fileIn = new Scanner(hours);
numberOfEmployees = fileIn.nextInt();
while (fileIn.hasNext()) {
for (int i = 0; i < numberOfEmployees; i++) {
for (int a = 0; a < 7; a++) {
int num = fileIn.nextInt();
array[i][a] = num;
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(0);
}
// takes employees hour total
for (int i = 0; i < 10; i++) {
total[i] = array[i][0] + array[i][1] + array[i][2] + array[i][3]
+ array[i][4] + array[i][5] + array[i][6];
}
// takes the hours and sorts from greatest to least
for (int i = 0; i < 7; i++) {
int greatest = total[i];
for (int b = i + 1; b < 10; b++) {
if (total[b] > greatest) {
int employeeTemp = employees[i];
employees[i] = employees[b];
employees[b] = employeeTemp;
int tempNum = greatest;
greatest = total[b];
total[i] = greatest;
total[b] = tempNum;
}
}
}
// print employee number and worked hours
for (int i = 0; i < 10; i++) {
System.out.println(" Employee #" + employees[i] + ": " + total[i]);
}
}
}
输出
Employee #5: 40
Employee #0: 39
Employee #9: 38
Employee #6: 36
Employee #3: 35
Employee #2: 28
Employee #7: 27
Employee #1: 26
Employee #8: 19
Employee #4: 27
您的代码中有两个问题。 你把代码放在catch块中。 2.文件构造函数中的文件名是&#34; prog2.dat&#34;但实际上是&#34; prog 2.dat&#34;
答案 2 :(得分:0)
您的代码的主要问题是您的程序逻辑保存在catch块中。
因为看起来你对try-catch概念不是很熟悉。 Try block是程序尝试执行可能引发异常的地方的地方。它也可能不会。如果它确实如此 - 为此您有捕获块。这称为异常处理。如果出现问题并且程序在try块中抛出异常,则执行将在catch块中继续执行,例如,您可以打印出一个人类可读的异常并继续执行其他操作,而不会在运行时停止执行程序异常。
所以 - 在你的情况下 - 你不希望你的程序的逻辑在catch块中。基本上你的代码现在会发生什么:
您要做的是将您的逻辑放在try块中,以便在读取文件后执行,继续操作读入的数据。
有关try-catch的更多信息: http://docs.oracle.com/javase/tutorial/essential/exceptions/try.html http://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html
您知道 - 当您只执行上述操作时,您将获得InputMismatchException,因为该文件在第一行包含10。我假设这是随后的案例数量 - 使用此值初始化数组大小(在进入while循环之前)可能是个好主意。