这是有问题的代码:
public class GradeBookRunner
{
public static void main( String args[] )
{
out.println("Welcome to the Class Stats program!");
int num = 1;
int count = 0;
String stuGrades = "";
String stuName = "";
Scanner keyboard = new Scanner(System.in);
out.print("What is the name of this class? ");
String clsName = keyboard.nextLine();
out.println("\n");
out.print("How many students are in this class? ");
int clsNum = keyboard.nextInt();
Class CSAB = new Class(clsName, clsNum);
out.println("\n");
out.print("Enter the name of student 1 : ");
stuName = keyboard.nextLine();
keyboard.next();
out.print("Enter the grades for " + stuName + "\nUse the format x - grades ( 2 - 100 100) : ");
//count = keyboard.nextInt();
//keyboard.next();
//for(int i = 0; i < count; i++)
//{
stuGrades += keyboard.nextLine() + " ";
//}
Student add = new Student(stuName,stuGrades);
CSAB.addStudent(num, add);
stuGrades = "";
num++;
out.println();
这个使用的类的代码可以在以下链接中找到:
Grades.java:http://pastebin.com/ahYRS2WD _
Student.java:http://pastebin.com/EBF4BBCb _
类.java: http ://pastebin.com/A1C9fCL1
我的问题发生在我运行代码并输入班级名称、班级人数、学生姓名之后,这些都很好,当我输入学生的成绩时,下一点会引发错误,例如3 - 70.2 65.3 45.1
编辑 这是我得到的错误:
欢迎来到班级统计计划!这个班级的名字是什么?ap
这个班有多少学生?1
输入学生 1 的姓名:几乎没有
输入成绩很难
使用格式 x - 等级 (2 - 100 100):线程“主”中的异常
java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at lab19b.Grades.setGrades(Grades.java:31)
at lab19b.Grades.<init>(Grades.java:25)
at lab19b.Student.<init>(Student.java:27)
at lab19b.GradeBookRunner.main(GradeBookRunner.java:42)
编辑2
我现在认为问题是我需要stuGrades
阅读整行,例如3 - 70.2 65.3 45.1
但我无法弄清楚我需要将键盘输入放入字符串的哪个扫描仪语句
编辑3
这是我正常工作的证据grades.java
:
package lab19b;
import java.util.Arrays;
import java.util.Scanner;
import static java.lang.System.*;
import static java.util.Arrays.*;
public class GradesTester
{
public static void main( String args[] )
{
Grades test = new Grades("5 - 90 85 95.5 77.5 88");
out.println(test);
out.println("sum = " + test.getSum());
out.println("num grades = " + test.getNumGrades());
out.println("low grade = " + test.getLowGrade());
out.println("high grade = " + test.getHighGrade()+ "\n\n");
test.setGrades("9 - 10 70 90 92.5 85 95.5 77.5 88 100.0");
out.println(test);
out.println("sum = " + test.getSum());
out.println("num grades = " + test.getNumGrades());
out.println("low grade = " + test.getLowGrade());
out.println("high grade = " + test.getHighGrade());
输出如下: 90.0 85.0 95.5 77.5 88.0
总和 = 436.0
成绩数 = 5
低等级 = 77.5
高等级 = 95.5
10.0 70.0 90.0 92.5 85.0 95.5 77.5 88.0 100.0
总和 = 708.5
成绩数 = 9
低等级 = 10.0
高等级 = 100.0