【问题标题】:java- return integer value of average score?java-返回平均分的整数值?
【发布时间】: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 规则。

标签: java swing bluej


【解决方案1】:

在 Student 中,您应该将 score1、score2 和 score3 作为私有实例变量放在顶部,并为它们设置 getter 和 setter,就像为 firstName 和 lastName 所做的那样。您可以为每个设置单独的 getter 和 setter,但有多种方法可以做到这一点。

您可以从 getAverageScore 返回一个 int 值,例如:

public int getAverageScore() {
    float ans = (score1 + score2 + score3)/3.0f;
    return Math.round(ans);

我使用了浮点数,因为带有双精度的 Math.round 会返回一个长整数。

在 main 方法中,您可以使用 setScores() 方法设置分数。

student1.setScore1(score1);
student1.setScore2(score2);
student1.setScore3(score3);

要打印学生,您可以在 Student 中创建一个 toString 方法,或者您可以使用您的 getter 来打印它们。 toString 方法如下所示:

public String toString() {
    return firstName + " " + lastName + " " + score1 + " " + score2 + " " + score3;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多