0

这是有问题的代码:

 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

4

1 回答 1

0

setGrades(String gradeList)您的问题在于Grades 中的 public void功能Line 27 这意味着整个gradeList类似于“ab c”。将此转换为数组函数并读取单个字符串。[a],[b],[c]。使用splitString 的函数来形成一个数组并填充你的grades[i]

Scanner 中的这种方法导致了这个问题,这意味着没有输入可以解析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     }
于 2013-02-28T04:07:23.290 回答