简体   繁体   English

如何在课堂上传递这些变量以计算新的GPA?

[英]How do I pass these variables to calculate a new GPA in my class?

sorry for this very similar question to my last post, but I'm having a slightly different problem to my last question. 非常抱歉,这个问题与我的上一个帖子非常相似,但是我遇到的问题与我的上一个问题稍有不同。 I have gotten everything to compile and work in my project, but I am not sure how to calculate a new gpa for student1 and student2 in my class by passing a total amount of points and a total amount of classes. 我已经获得了在项目中可以编译和工作的所有内容,但是我不确定如何通过传递总点数和总班级数来为班级中的Student1和Student2计算新的gpa。 My teacher wants us to calculate student1's gpa by passing 40 total points for 7 classes and to calculate student2's gpa by passing 36 total points for 8 classes. 我的老师希望我们通过7个班的总得分40计算学生1的gpa,并通过8个班的总得分36计算学生2的gpa。 Like I said, the rest of my code works, I'm just not sure how to do this yet as I'm still fairly new to coding. 就像我说的那样,我的其余代码都可以正常工作,但我仍然不确定该如何做,因为我对编码还很陌生。 The answer is probably pretty simple, but I can't figure it out for the life of me. 答案可能很简单,但我无法终生解决。 Here is my code: 这是我的代码:

public class student
{
   private String name;
   private int year;
   private int age;
   private double gpa;
   /*
    * Default Constructor
    */
   public student()
   {
       name = "John";
       year = 2016;
       age = 16;
       gpa = 4.0;
   }
   /* 
    * other constructor
    */
   public student(String name, int year, int age, double gpa)
   {
       this.name = name;
       this.year = year;
       this.age = age;
       this.gpa = gpa;
   }
   /*
    * accessors
    */
   public String getName()
   {
       return name; 
   }
   public int getAge()
   {
       return age;
   }
   public int getYear()
   {
       return year;
   }
   public double getGpa()
   {
       return gpa;
   }
   /*
    * mutators
    */
   public void setName(String name)
   {
       this.name=name;
   }
   public void setGpa(double gpa)
   {
       this.gpa=gpa;
   }
   public void setYear(int year)
   {
       this.year=year;
   }
   public void setAge(int age)
   {
       this.age=age;
   }
   /*
    * calculate GPA
    */
   public double calcGpa(double points, int classes)
   {
      gpa = points / classes;       
      return gpa; 
   }
   public String toString() 
   {
    return name + year + age + gpa;
   }
}



public class studentReal
    {
       public static void main(String[] args)
       {
           student student1= new student("Jackie Java", 2018, 16, 4.7);
           student student2= new student("John Java", 2019, 15, 3.8);
           student student3= new student();
           System.out.println(student1.getName() + ", class " + student1.getYear()+ ", " + student1.getAge() + ", " + student1.getGpa());
           System.out.println(student2.getName() + ", class " + student2.getYear()+ ", " + student2.getAge() + ", " + student2.getGpa());
           System.out.println(student3.getName() + ", class " + student3.getYear()+ ", " + student3.getAge() + ", " + student3.getGpa());
       }
    }

To do this with the functions you have currently I would go this route. 为此,请使用当前的功能。

double newGpa = student.gpaCalc(<any number>, <any number>);
student.setGpa(newGpa);

Your other options are in the calcGpa function, call the setGpa function rather than returning the value, or set it within the function like so: 您的其他选项在calcGpa函数中,调用setGpa函数而不是返回值,或者在函数内进行设置,如下所示:

public double calcGpa(double points, int classes)
   {
      gpa = points / classes;       
      this.gpa = gpa 
}

In your studentReal class, inside the main method, use the calcGpa method you created within the Student class, for each student. 在您的studentReal类的main方法内,对每个学生使用您在Student类中创建的calcGpa方法。 The method, just like the variables, belongs to that student. 该方法就像变量一样,属于该学生。

For example: 例如:

    student1.calcGpa(40, 7);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用toString检索我的班级中的信息,以及如何获取用于计算此类中的“ GPA”的方法? - How do I use toString to retrieve my information in my class and how do I get my method to calculate the “GPA” in this class? 如何在整个类中使用变量并将变量传递到新的android活动中? - How do I pass variables into a new android activity, with the ability to use them throughout the class? 如何从我的班级变量计算总和? - How to calculate a sum from my class variables? 如何在 clojure 中将 class 间接传递给 new - How do I pass a class to new indirectly in clojure 如何在此 GPA 计算器中构建我的 for 循环? 爪哇 - How can I build my for loop in this GPA Calculator? Java 如何获得循环以保持重新输入新变量和运算符以继续计算? - How can I get my loop to keep re-entering new variables and operators to continue to calculate? 我如何计算我的代码的均值 - how do i calculate the mean for my code Java:如何将变量从JButton ActionListener传递给主类? - Java: How do I pass variables from JButton ActionListener to main class? 如何在派生类中重写`toString`并使用基类中的私有实例变量? - How do I override `toString` in my derived class and use the private instance variables from the base class? 按下按钮后,如何使GUI驱动程序类中的变量转移到ActionListener类中? - How do I make the the variables in my GUI Driver class to be transferred to the ActionListener class after a button is pressed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM