0
我無法弄清楚我的編碼有什麼問題。它需要從文件中讀取文本並打印平均值,最大值和最小值。從檔案中讀取成績
import java.util.*;
import java.io.*;
public class LoopAndHalf{
public static void main(String[]args)throws FileNotFoundException{
Scanner input = new Scanner(new File("midterm.txt"));
int lineNo=0;
int id=0;
int grade =0;
String name;
int min=1000;
int max=-1000;
int sum=0;
int count=0;
int[] scale = new int[6];
while(input.hasNextLine()){
input.nextLine();
lineNo++;
Scanner lineScan = new Scanner(line);
id = lineScan.nextInt();
name = lineScan.next();
grade = lineScan.nextInt();
count++;
if(grade>max){
max=grade;
}
if(grade<min){
min=grade;
}
if(grade>=96){
scale[5]++;
}else if(grade>=91){
scale[4]++;
}else if(grade>=86){
scale[3]++;
}else if(grade>=81){
scale[2]++;
}else if(grade>=76){
scale[1]++;
}else{
scale[0]++;
}
}//end while loop
System.out.println("Scale array: " + Arrays.toString(scale));
System.out.println("Count: " + count);
double avg= (double) sum/count;
System.out.println("Min: " + min);
System.out.println("Max: " + max);
System.out.println("Avg: " + avg);
//draw histogram
System.out.println();
for(int i=0; i<scale.length; i++){
if(i==0){
System.out.println(" - 75: ");
}else if(i==1){
System.out.println("76 - 80: ");
}else if(i==2){
System.out.println("81 - 85: ");
}else if(i==3){
System.out.println("86 - 90: ");
}else if(i==4){
System.out.println("91 - 95: ");
}else{
System.out.println("95 + : ");
}
}//end for loop
int[] counts = new int[101]; // counters of test scores 0 - 100
while (input.hasNextInt()) { // read file into counts array
int score = input.nextInt();
counts[score]++; // if score is 87, then counts[87]++
}
for (int i = 0; i < counts.length; i++) { // print star histogram
if (counts[i] > 0) {
System.out.print(i + ": ");
for (int j = 0; j < counts[i]; j++) {
System.out.print("*");
}
System.out.println();
}
}
}//end main
}//end class
是不斷給我找麻煩該生產線是
Scanner lineScan = new Scanner(line);
錯誤是告訴我,行不能被解析爲一個變量,但是這是導致我的問題的唯一的事。 任何指針,將不勝感激。謝謝
的錯誤意味着正是它說。您沒有一個名爲line的變量,因此您無法將其作爲參數傳遞給Scanner的構造函數。 – Daniel 2014-10-28 20:29:50
你應該使用緩衝區閱讀器 – KRUKUSA 2014-10-28 20:30:26