我正在为一个使用文件数据(学生成绩)的课程编写程序,然后根据最高等级到最低等级对其进行平均和组织。我几乎完成了,除了第46行的错误,它不会调用方法:
findMaxIndex(courseGrade)
;
非常感谢任何帮助!
package project3;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
*
* @author badluckbowers
*/
public class Project3 {
final static int NUM_STUDENTS = 16;
public static String[] nameArray = new String[NUM_STUDENTS];
public static double[] labAvg = new double[NUM_STUDENTS];
public static double[] quizAvg = new double[NUM_STUDENTS];
public static double[] projectAvg = new double[NUM_STUDENTS];
public static double[] examAvg = new double[NUM_STUDENTS];
public static double[] finalExamArray = new double[NUM_STUDENTS];
public static double[] courseGrade = new double[NUM_STUDENTS];
public static char[] gradeArray = new char[NUM_STUDENTS];
/**
* @param args the command line arguments
*/
final static int numLabScores = 15;
final static int pointsLabPossible = 10;
final static int numQuizScores = 12;
final static int pointsQuizPossible = 5;
final static int numProjectScores = 6;
final static int pointsProjectPossible = 25;
final static int numExamScores = 2;
final static int pointsExamPossible = 100;
final static int numFinalExamScores = 1;
final static int pointsFinalExamPossible = 100;
public static void main(String[] args) throws FileNotFoundException {
readFile("scores.txt", nameArray, labAvg, quizAvg, projectAvg, examAvg, finalExamArray);
findMaxIndex(courseGrade);
printArray();
}
public static void readFile(String fileName, String[] nameArray, double[] labAvg, double[] quizAvg, double[] projectAvg, double[] examAvg, double[] finalExamArray) throws FileNotFoundException {
File input = new File(fileName);
if (!input.exists()) {
System.out.println("Error opening scores.txt for input; "
+ "aborting program run.");
System.exit(0);
}
Scanner inputFile = new Scanner(input);
for (int i = 0; i < NUM_STUDENTS; i++) {
nameArray[i] = inputFile.nextLine();
labAvg[i] = calculatePercent(inputFile, numLabScores, pointsLabPossible); //15-1
quizAvg[i] = calculatePercent(inputFile, numQuizScores, pointsQuizPossible); //12-1
projectAvg[i] = calculatePercent(inputFile, numProjectScores, pointsProjectPossible); //6-1
examAvg[i] = calculatePercent(inputFile, numExamScores, pointsExamPossible); //2-1
finalExamArray[i] = calculatePercent(inputFile, numFinalExamScores, pointsFinalExamPossible); //1-1
courseGrade[i] = calculateGrade(labAvg[i], quizAvg[i], projectAvg[i], examAvg[i], finalExamArray[i]);
gradeArray[i] = calculateLetter(courseGrade[i]);
inputFile.nextLine();
}
inputFile.close();
}
public static double calculatePercent(Scanner inFile, int numScores, int pointsPossible) {
double score;
double total = 0;
for (int i = 0; i < numScores; i++) {
score = inFile.nextDouble();
total += score;
}
return (total / (numScores * pointsPossible)) * 100;
}
public static double calculateGrade(double labAvg, double quizAvg, double projectAvg, double examAvg, double finalExamArray) {
return ((labAvg * .15 + quizAvg * .10 + projectAvg * .25 + examAvg * .30 + finalExamArray * .20));
}
public static char calculateLetter(double courseGrade) {
if (courseGrade < 60.0) {
return 'F';
} else if (courseGrade >= 60.0 && courseGrade < 70.0) {
return 'D';
} else if (courseGrade >= 70.0 && courseGrade < 80.0) {
return 'C';
} else if (courseGrade >= 80.0 && courseGrade < 90) {
return 'B';
}
return 'A';
}
//__________________________________________________________________________
// sort stuff
/**
* finds index of smallest element in the part of the array bounded by start
* and array.length-1
*
* @param courseGrade is fully populated
* @param start is index to begin searching for smallest element
* @return index of smallest item between start and array.length-1
* @throws java.io.FileNotFoundException
*/
public static double findMaxIndex(double[] courseGrade, int start) throws FileNotFoundException {
int maxIndex;
maxIndex = start;
for (int index = start + 1; index < courseGrade.length; index++) {
if (courseGrade[index] > courseGrade[maxIndex]) {
maxIndex = index;
}
}
return maxIndex;
}
/**
* the items in the array will be sorted largest to smallest
*
* @param courseGrade
* @throws java.io.FileNotFoundException
*/
public static void sortArray(double[] courseGrade) throws FileNotFoundException {
int maxIndex;
double maxItem;
for (int i = 0; i > courseGrade.length - 1; i++) {
maxIndex = (int) findMaxIndex(courseGrade, i);
//put the smaller item in place of the current item
maxItem = courseGrade[maxIndex];
courseGrade[maxIndex] = courseGrade[i];
courseGrade[i] = maxItem;
System.out.println("Array after pass " + (i + 1) + " of selection sort:");
for (int j = 0; j < courseGrade.length; j++) {
System.out.print(courseGrade[j] + " ");
}
System.out.println("");
}
}
//__________________________________________________________________________
public static void printArray() {
System.out.println("The array elements are: ");
for (int i = 0; i < nameArray.length; i++) {
System.out.println(nameArray[i] + " " + labAvg[i] + " " + quizAvg[i] + " " + projectAvg[i] + " " + examAvg[i] + " " + finalExamArray[i] + " " + courseGrade[i] + " " + gradeArray[i]);
}
System.out.println();
}
}
答案 0 :(得分:1)
调用函数 findMaxIndex(courseGrade, startIndex);
时需要传递第二个参数。因为你的函数定义有两个参数。
def connect_db():
conn = sqlite3.connect(app.config['DATABASE'])
conn.row_factory = sqlite3.Row
return conn
def init_db():
conn = connect_db()
cursor = conn.cursor()
sql = 'create table if not exists users (id integer primary key autoincrement, username text not null, password text not null, admin boolean not null)'
cursor.execute(sql)
conn.commit()