我们正在编写一个名为GradeBook的程序,它允许用户输入学生数据,例如姓名和不同年级项目的分数。GradeBook为每个学生提供选项计算成绩并绘制班级成绩分布。用户可以随时返回并添加更多学生数据。它支持的最大学生人数为200人,一个类别中成绩项目的最大数量为10人。例如,它最多只支持10次测验、10次考试和10次家庭作业。
输入数据的示例如下所示:
乔·史密斯:e100 e95 e87 q10 q10 q8 h10 h10 h10 h10
迈克尔·布朗:q10 q10 h7 h10 h9 h10 e80
为了显示学生的成绩和统计数据,我们应该使用System. out.printf。
这是最终输出应该是什么的另一个示例。
Name Exam Exam Exam Quiz Quiz Quiz HWork HWork HWork Grade
Danny Devito 100.0 80.0 90.0 10.0 10.0 0.0 10.0 5.0 10.0 84.0
Joe Smith 85.0 90.0 100.0 10.0 10.0 5.0 0.0 10.0 5.0 81.7
Will Smith 60.0 100.0 90.0 10.0 10.0 8.0 10.0 0.0 10.0 82.0
最后一个例子应该排列得更好,并与小数对齐,这样看起来整洁干净,但我不确定如何做到这一点。
import java.util.Scanner;
public class GradeCalcWellArray { /* * Logan Wegner目的是计算*输入的成绩*/public静态无效主(String[]args){
Scanner s = new Scanner(System.in);
boolean done = false;
boolean quit = false;
int choice = 0;
int studentcounter = 0;
int[] examstats = new int[3]; /*
* Array created to store the information
* entered for exams
*/
int[] quizstats = new int[3]; /*
* Array created to store the information
* entered for quizzes
*/
int[] homeworkstats = new int[3]; /*
* Array created to store the
* information entered for homework
*/
String[] studentnames = new String[200]; /*
* Array created to store the
* student name information
* entered
*/
System.out.println("Welcome to GradeBook!");
System.out.println("Please provide grade item details");
System.out.print("Exams (number, points, weight):");
examstats[0] = s.nextInt(); // inputs exam number
examstats[1] = s.nextInt(); // inputs exam points
examstats[2] = s.nextInt(); // inputs exam weight
System.out.print("Quizzes (number, points, weight):");
quizstats[0] = s.nextInt(); // inputs quiz number
quizstats[1] = s.nextInt(); // inputs quiz points
quizstats[2] = s.nextInt(); // inputs quiz weight
System.out.print("Homework (number, points, weight):");
homeworkstats[0] = s.nextInt(); // inputs homework number
homeworkstats[1] = s.nextInt(); // inputs homework points
homeworkstats[2] = s.nextInt(); // inputs homework weight
System.out.println("--------------------");
do {
System.out.println("What would you like to do?");
System.out.println(" 1 Add student data");
System.out.println(" 2 Display student grades & statistics");
System.out.println(" 3 Plot grade distribution");
System.out.println(" 4 Quit");
System.out.print("Your choice:");
choice = s.nextInt(); /*
* Choice will determine what the next course of
* action will be with the program
*/
if (choice == 1) {
System.out.println("Enter student data:");
for (int i = 0; i <= 200; i++) {
studentcounter = studentcounter + 1;
System.out.print("Data>");
studentnames[i] = s.nextLine();
if (studentnames[i].equals("done")) {
break;
}
}
}
if (choice == 2) {
}
if (choice == 3) {
}
if (choice == 4) {
quit = true;
System.out.println("Good bye!");
}
} while (quit == false);
}
}
我遇到的最大问题是能够输入数据并将其放入字符串和数组中。我不确定如何使用e100 q100 h100输入数据,因为它们可能会混淆和乱序。我真的非常非常感谢一些帮助。提前谢谢伙计们。
试着把你的问题和你真正困惑的事情分开。通常这会让你弄清楚你需要做什么。
看起来你真的不确定如何将考试
、测验
和作业
字符串分成3个单独的数组,因为它们没有任何顺序。好吧,要分离它们,它们必须有区别。这三种类型的字符串有什么区别?
所有三种类型的字符串输入都可以在一个字母上附加0到100之间的数字:“e”、“q”或“h”。所以唯一的区别是每个字符串的第一个字母。因此,要将所有不同的字符串分成单独的数组,您需要检查第一个字母是什么,并相应地将其放入相应的数组中。
现在您终于到了需要编码的地步:如何根据字符串的第一个字母以编程方式分离字符串?
好吧,您有if
语句,我相信您可以弄清楚如何获取字符串的第一个字符。
用文字(又名伪代码)列出:
String s;
Char c;
c = //get the first letter of string 's'. Figure this out with a simple google search.
if (c == 'e')
{
//Add the string after the first character in string s to the Exam array
//NOTE: You have int arrays, so you will need to convert the string to an int
// before adding it to the array.
}
else if (c == 'q')
{
//Same as above with Quiz array
}
else if ...
{
//... you should be able to finish this!
}
这应该会让你开始你的项目。在你剩下的作业中,试着模仿上面详述的思考过程,你会很快发现你在初学者阶段遇到的每一个问题都已经解决了;你只需要找出你在编程方面的具体问题。