我正在开发一个学生成绩应用程序,它接受一个或多个学生的姓氏、名字和分数,并将结果存储在一个数组中。然后,它按姓氏的字母顺序打印学生及其分数。我们不知道有多少学生,但会少于100人。
我们必须在学生信息的末尾显示班级平均水平,并在每个成绩低于班级平均水平10分以上的学生之后显示一条消息。
我的第一个问题是,我创建了一个do/while循环来询问用户是否想输入另一个,但它不会工作!?!?
其次,我不知道如何在每个学生身上显示“以下10点”的信息。
public class Student implements Comparable
{
String firstName;
String lastName;
int score;
//stores last name, first name and score for each student
public Student(String lastName,String firstName,int score)
{
this.lastName = lastName;
this.firstName = firstName;
this.score = score;
}
//implement the comparable interface so students can be sorted by name
public int compareTo(Object o)
{
Student otherStudent = (Student)o;
if(otherStudent.lastName.equals(lastName))
{
return firstName.compareToIgnoreCase(otherStudent.firstName);
}
else
{
return lastName.compareToIgnoreCase(otherStudent.lastName);
}
}
public String toString()
{
return lastName + ", " + firstName + ": " + score;
}
}
import java.util.Scanner;
import java.util.Arrays;
public class StudentApp
{
static Scanner sc = new Scanner(System.in);
public static void main(String [] args)
{
Student [] studentArray;
String lastName;
String firstName;
int score = 0;
double average = 0;
System.out.println("Welcome to the Student Scores Application.");
System.out.println();
do{
//code that uses variable to specify the array length
int nStudent = 100; //array size not set unit run time
studentArray = new Student[nStudent];
for (int i=0; i<nStudent; i++)
{
System.out.println();
lastName = Validator.getRequiredString(sc,
"Student " + (i+1) + " last name: ");
firstName = Validator.getRequiredString(sc,
"Student " + " first name: ");
score = Validator.getInt(sc,
"Student " + " score: ",
-1, 101);
studentArray[i] = new Student(lastName, firstName, score);
double sum = 0.0;
sum += score;
average = sum/nStudent;
}
}while (getAnotherStudent());
Arrays.sort(studentArray);
System.out.println();
for (Student aStudent: studentArray)
{
System.out.println(aStudent);
if (score<= (average-10))
{
System.out.println ("Score 10 points under average");
}
}
System.out.println("Student Average:" +average);
}
public static boolean getAnotherStudent()
{
System.out.print("Another student? (y/n): " );
String choice = sc.next();
if (choice.equalsIgnoreCase("Y"))
return true;
else
return false;
}
}发布于 2012-05-28 11:06:17
这里有几个问题:
studentArray和sum。这意味着当getAnotherStudent()为真时,你之前迭代过的所有数据都会被sum你只想实例化一次数组并求和一次。nStudent的结束条件。getAnotherStudent()做一些调整,这样你就可以阻塞数据,并在输入有效数据时等待-通过使用循环:public static boolean getAnotherStudent() { Scanner sc = new Scanner(System.in);System.out.print(“另一个学生?(y/n):“);if (sc.hasNext()) { String choice = sc.next();//此处的块-忽略所有不是"y”或"n“的输入,同时(!((choice.equalsIgnoreCase(”Y“) || choice.equalsIgnoreCase(”N“){ if (choice.equalsIgnoreCase("Y")) { return true;)System.out.print(“另一个学生?(y/n):“);choice = sc.next();}}返回false;//强制
发布于 2012-05-28 10:53:01
你的代码很接近,只有几个问题。do while循环不工作的原因是它里面有一个for循环。这意味着您将询问100名学生,然后再询问他们是否要添加另一个学生。你的总和是在这个循环中创建的,所以每次都会被重置。
最后,您不知道将添加多少学生,但您的代码假设将有100名学生。这意味着您不能使用for each循环遍历数组,因为有些循环可能为空。只需使用一个常规的for循环,直到您添加的学生的最后一个索引。以下是更改:
Student[] student = new Student[nStudent];
int studentCount = 0; //declear the counter outside the loop
double sum = 0.0; //declear the sum outside the loop
do {
System.out.println();
lastName = Validator.getRequiredString(sc,
"Student " + (i+1) + " last name: ");
firstName = Validator.getRequiredString(sc,
"Student " + " first name: ");
score = Validator.getInt(sc,
"Student " + " score: ",
-1, 101);
student[studentCount] = new Student(lastName, firstName, score);
sum += score; //increase the sum
studentCount++; //increment the counter
} while (studentCount < nStudent && getAnotherStudent()); //stop if the user says 'n' or we hit the maximum ammount
average = sum / studentCount; //work out the average outside the loop
System.out.println();
for (int i= 0; i< studentCount; i++ ) {
System.out.println(aStudent);
if (score <= (average - 10)) {
System.out.println("Score 10 points under average");
}
}
System.out.println("Student Average:" + average);
}发布于 2012-05-28 10:47:58
您的getAnotherStudent()方法应为:
System.out.print("Another student? (y/n): " );
if (sc.hasNext()) { // blocks until user entered something
String choice = sc.next();
if (choice.equalsIgnoreCase("Y"))
return true;
else
return false;
} else {
// won't come here
return false;
}https://stackoverflow.com/questions/10778707
复制相似问题