【发布时间】:2015-03-29 01:08:54
【问题描述】:
我正在尝试用 java 编写一个程序,该程序需要 3 个学生的姓名和分数,然后计算他们的平均分数。以下是我想要实现的目标的详细列表:
- 修改 Student 类,使其具有三个新的 int 类型实例变量,名为 score1、score2 和 score3。
- 修改 Student 类,使其具有一个名为 averageScore 的方法,该方法返回一个 int 值,该值是这三个分数的平均值。
- 修改 main 方法,为您已创建的每个学生对象输入三个分数。
- 以整数形式打印每个学生的姓名,后跟他们的平均分数。
StudentTester主课
import javax.swing.JOptionPane;
public class StudentTester {
public static void main(String args[]) {
Student student1 = new Student();
Student student2 = new Student();
Student student3 = new Student();
String firstName;
String lastName;
//start first student here
firstName = JOptionPane.showInputDialog("Enter the student's first name.");
student1.setFirstName(firstName);
lastName = JOptionPane.showInputDialog("Enter the student's last name.");
student1.setLastName(lastName);
JOptionPane.showMessageDialog(null, "Student 1 has name "
+ student1.getFirstName() + ""+ student1.getLastName() + ".");
score1 = JOptionPane.showInputDialog("Enter the student's score.");
student1.setScore(score1);
int student1; getScore();
//start second student here
firstName = JOptionPane.showInputDialog("Enter the student's first name.");
student2.setFirstName(firstName);
lastName = JOptionPane.showInputDialog("Enter the student's last name.");
student2.setLastName(lastName);
JOptionPane.showMessageDialog(null, "Student 2 has name "
+ student2.getFirstName() + "" + student2.getLastName() + ".");
score2 = JOptionPane.showInputDialog("Enter the student's score. ");
student2.setScore(score2);
//start third student here
firstName = JOptionPane.showInputDialog("Enter the student's first name.");
student3.setFirstName(firstName);
lastName = JOptionPane.showInputDialog("Enter the student's last name.");
student3.setLastName(lastName);
JOptionPane.showMessageDialog(null, "Student3 has name "
+ student3.getFirstName() + "" + student1.getLastName() + ".");
//average score
}
}
学生班
public class Student {
private String firstName;
private String lastName;
public int score1;
public int score2;
public int score3;
public void setFirstName(String name){
firstName = name;
}
public String getFirstName(){
return firstName;
}
public void setLastName(String name){
lastName = name;
}
public String getLastName(){
return lastName;
}
public String setScore(){
int score1 = Integer.parseInt(score1Store);
}
//method for average score computation
public void score1 = Integer.parseInt(Score);
public double getAverageScore(){
return double = ( score1 + score2 + score3)/3.0;
}
}
我做错了什么?我完全迷失了,不确定如何设置整个分数输入和计算。
谢谢。
【问题讨论】:
-
您需要添加(并调用)
setScore1(int)、setScore2(int)和setScore3(int),您现在有三个分数......不是一个。 -
StackOverflow 不是为了让人们为你做作业。如果你完全迷失了,你需要回到基本原则并研究底层语法。
-
@Zaphod42 粗鲁,我只是在问一些方向。
-
@bretth18 粗鲁?尝试阅读 stackoverflow 规则。