以下是相关代码:
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
Class.java:http://pastebin.com/A1C9fCL1
我的问题发生在我运行代码并为类名,类大小,学生名称提供输入之后,这些都很好,这是下一个引发错误的部分,当我输入学生的成绩时如3 - 70.2 65.3 45.1
修改 这是我得到的错误:
欢迎来到Class Stats计划! 这门课的名字是什么? AP
这堂课有多少学生? 1
输入学生1的名字:几乎
输入难度
的成绩使用格式x - grade(2 - 100 100):线程“main”中的异常
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)
EDIT2
我现在认为问题是我需要stuGrades
来读取整个行,例如3 - 70.2 65.3 45.1
,但我无法弄清楚我需要将键盘输入放到哪个扫描程序语句中一个字符串
EDIT3
以下是我正确工作的证据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
sum = 436.0
num grades = 5
低等级= 77.5
高等级= 95.5
10.0 70.0 90.0 92.5 85.0 95.5 77.5 88.0 100.0
sum = 708.5
num grades = 9
低等级= 10.0
高等级= 100.0
答案 0 :(得分:0)
您的问题在于成绩setGrades(String gradeList)
中的公开无效Line 27
功能
这意味着整个gradeList
就像“a b c”。将其转换为数组函数并读取单个String。 [A],[B],[C]。使用String的split
函数形成一个数组并填充grades[i]
扫描程序中的此方法导致问题,这意味着在scan.nextInt()
之后没有任何输入要解析;
// If we are at the end of input then NoSuchElement;
903 // If there is still input left then InputMismatch
904 private void throwFor() {
905 skipped = false;
906 if ((sourceClosed) && (position == buf.limit()))
907 throw new NoSuchElementException();
908 else
909 throw new InputMismatchException();
910 }