Java:使用变量指定数组长度的编码?
我正在开发一个学生成绩应用程序,它接受一个或多个学生的姓、名和成绩,并将结果存储在一个数组中。然后它按姓氏的字母顺序打印学生和他们的分数。我们不知道有多少学生,但人数将少于100人 我们必须在学生信息的末尾显示班级平均成绩,并在每个成绩低于班级平均成绩10分以上的学生之后显示一条消息 我的第一个问题是,我创建了一个do/while循环,询问用户是否想输入另一个,但它不会工作 第二,我不知道如何在个别学生身上显示“10分以下”的信息Java:使用变量指定数组长度的编码?,java,arrays,Java,Arrays,我正在开发一个学生成绩应用程序,它接受一个或多个学生的姓、名和成绩,并将结果存储在一个数组中。然后它按姓氏的字母顺序打印学生和他们的分数。我们不知道有多少学生,但人数将少于100人 我们必须在学生信息的末尾显示班级平均成绩,并在每个成绩低于班级平均成绩10分以上的学生之后显示一条消息 我的第一个问题是,我创建了一个do/while循环,询问用户是否想输入另一个,但它不会工作 第二,我不知道如何在个别学生身上显示“10分以下”的信息 public class Student implements
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;
}
}
公共班级学生实施可比
{
字符串名;
字符串lastName;
智力得分;
//存储每个学生的姓、名和分数
公立学生(字符串lastName、字符串firstName、整数分数)
{
this.lastName=lastName;
this.firstName=firstName;
这个分数=分数;
}
//实现可比较的界面,以便学生可以按姓名排序
公共整数比较对象(对象o)
{
学生其他学生=(学生)o;
if(otherStudent.lastName.equals(lastName))
{
返回firstName.compareTignoreCase(otherStudent.firstName);
}
其他的
{
返回lastName.compareTignoreCase(otherStudent.lastName);
}
}
公共字符串toString()
{
返回lastName+“,“+firstName+”:“+score;
}
}
导入java.util.Scanner;
导入java.util.array;
公共班级学生pp
{
静态扫描仪sc=新扫描仪(System.in);
公共静态void main(字符串[]args)
{
学生[]学生阵列;
字符串lastName;
字符串名;
智力得分=0;
双平均=0;
System.out.println(“欢迎使用学生成绩应用程序”);
System.out.println();
做{
//使用变量指定数组长度的代码
int nStudent=100;//数组大小未设置单位运行时
学生阵列=新生[学生];
对于(int i=0;i您的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;
}
你的代码很接近,只是有几个问题。do-while循环不起作用的原因是它里面有一个for循环。这意味着,在你询问学生是否要添加另一个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);
}
Student[]Student=新学生[nStudent];
int studentCount=0;//清除循环外的计数器
double sum=0.0;//清除循环外的和
做{
System.out.println();
lastName=Validator.getRequiredString(sc,
“学生”+(i+1)+“姓氏:”;
firstName=Validator.getRequiredString(sc,
“学生”+“名字:”);
分数=Validator.getInt(sc,
“学生”+“分数:”,
-1, 101);
学生[studentCount]=新学生(姓、名、分数);
总和+=分数;//增加总和
studentCount++;//递增计数器
}while(studentCount
- 每次通过do…while重新实例化
studentArray
和sum
。这意味着当getAnotherStudent()
为true时,所有先前迭代的数据都会被忽略-您只想实例化数组并求和一次
- 如果学生人数超过100人,则不会停止。循环中还需要在
nStudent
周围设置结束条件
- 您应该对
getAnotherStudent()
进行一些调整,以便在输入有效数据时阻塞数据并等待-通过使用循环:
public static boolean getAnotherStudent() {
Scanner sc = new Scanner(System.in);
System.out.print("Another student? (y/n): " );
if (sc.hasNext()) {
String choice = sc.next();
// blocks here - ignores all input that isn't "y" or "n"
while(!((choice.equalsIgnoreCase("Y") || choice.equalsIgnoreCase("N")))) {
if (choice.equalsIgnoreCase("Y")) {
return true;
}
System.out.print("Another student? (y/n): " );
choice = sc.next();
}
}
return false; // obligatory
如果分配需要一个数组,则不应偏离该值。解决该问题。由于更改,将ArrayList更改为数组,并将每个数组的结尾更改为正常的for循环。它将等待用户输入某个内容,但当我输入“m”而不是“n”时中止设计是错误的正如指定的那样。@Makoto,如果你认为这是个好主意,你可以编辑答案来限制用户输入的接受。但是,我更喜欢一个不会一直困扰我的应用程序,因为我没有准确地键入它期望的内容,而不是在这种情况下只使用一些默认值……我不会编辑你的答案回答。如果你觉得这样很好,那么你可以不理会那些否决你的人。我只是对可用性和设计有强烈的感觉;如果我想通过键入“n”来取消,它应该只对“n”做出响应,而不是对“6”。但是,如果您对它感到满意,请不要担心。当用户输入“n”时,您的阻止代码位于循环中。如果您将while循环更改为| |,并将if移到循环外,则可以正常工作。我很惊讶我没有注意到。谢谢@Glitch。