【发布时间】:2014-11-30 06:03:51
【问题描述】:
有人可以帮我处理这些代码吗?
这是文件
57363 Joy Ryder D D C P H H C D
72992 Laura Norder H H H D D H H H
71258 Eileen Over C F C D C C C P
我正在尝试将 ID 存储在 5 位数组中,并将字符转换为数字并计算 H=7、D=6、C=6、P=4、F=0 的平均 gpa 并存储这在第二个数组中。然后将这两个数组从最大值到最小值排序,并将这些值写入输出文本文件中。这是我到目前为止的代码
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class StudentGPA_17396934Demo {
public static void main(String[] args) throws IOException {
Scanner kb = new Scanner(System.in);
String fileName;
char grade[] = new char[8];
File myfile;
int num = 0;
do{
System.out.println("Enter the name of the file: ");
fileName = kb.next();
myfile = new File(fileName);
if (myfile.exists()) {
Scanner infile = new Scanner(myfile);
while (infile.hasNext()) {
int ID = infile.nextInt();
String name = infile.next();
String surname = infile.next();
// it is converting the characters to numbers but not reading all characters
also how do I get average of this numbers and store in appropriate array
also how to store ID in array
for (int i = 0; i < grade.length; i++) {
grade[i] = infile.next().charAt(0);
if (grade[i] == 'H')
num = 7;
else if (grade[i] == 'D')
num = 6;
else if (grade[i] == 'C')
num = 5;
else if (grade[i] == 'P')
num = 4;
else if (grade[i] == 'F')
num = 0;
}
System.out.println(ID + "\t" +name + "\t" +surname + "\t" + num);
}
}
else{
System.out.println("file not found...");
}
}while(!myfile.exists());
}
}
我的输出是
Enter the name of the file:
grades
57363 Joy Ryder 6
72992 Laura Norder 7
71258 Eileen Over 4
【问题讨论】:
-
什么困扰着你?问题是什么?如果这是你的作业,没有人会为你做的:)
-
我不知道如何将 id 和 Grades 存储到数组中并写入文件
-
让您了解您的问题。首先如何存储在数组中,其次如何写入文件。所以试着用谷歌搜索它们并学习它们。如果您被困在某个地方,请告诉我们。你必须学会如何研究
-
我已经尝试了一个星期,但到目前为止没有运气..我根本无法理解
-
快点换个帖子,说你哪部分有问题,我们可以解释并帮助你理解:)