我有一个二维数组程序,我有一个问题。我必须在程序中读取一个存储有数字的文件。问题是有10名员工,我需要他们从周日到周六的每周总工作时间。
文件(prog .dat)是:
108 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。
为什么所有员工的输出都是0 ?
您可能已经缩进了:逻辑都在捕获中,这肯定不是您想要的。
写全部为零,因为实际上,流的执行将您直接带出系统,没有任何被利用的东西:没有异常
移动末尾的catch,你将得到
员工# 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();
}
}
代码正常运行
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
你的代码有两个问题。1. 你把代码放在catch块中。2. 文件构造函数中的文件名为"prog2.dat",但实际上是"prog2.dat"
你的代码的主要问题是你的程序逻辑被保存在catch块中。
看来你对try-catch的概念不太熟悉。Try块是程序尝试执行可能抛出异常的操作的地方。也可能不会。如果是这样,那么你就有了catch块。这被称为异常处理。如果出现错误,程序在try块中抛出异常,则在catch块中继续执行,例如,您可以打印出一个人类可读的异常,并继续执行其他操作,而不会因运行时异常而停止程序的执行。
所以——在你的例子中——你不希望你的程序逻辑在catch块中。基本上你的代码现在发生了什么:
- 文件正在被读取(在try块中)
- 没有抛出异常-因此在注释处继续执行(//打印员工编号和工作时间)。
- 名为total的数组为空-默认情况下Java int[]数组初始化为0。
你要做的是把你的逻辑放在try块中,这样在读取文件后的执行,继续对读取的数据进行操作。
更多关于试捕的信息:http://docs.oracle.com/javase/tutorial/essential/exceptions/try.htmlhttp://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html
正如您所知道的—当您只执行上述操作时,您将得到InputMismatchException,因为文件在第一行包含一个10。我假设这是接下来的情况的数量-也许用这个值初始化数组大小是一个好主意(就在进入while循环之前)。