gpt4 book ai didi

java - 将用户输入返回到 setter 函数中

转载 作者:行者123 更新时间:2023-12-01 16:14:41 26 4
gpt4 key购买 nike

我一直在做一项作业,但我被困在这里。基本上我有 1 个类,它定义了所有函数和成员。

还有另一个类来初始化和操作对象。

这是我的第一类代码。

public class cyryxStudent_association {

String studentID, studentName, studentCourse_level, studentTitle;
int course_completed_year;
static double registration_fee;
double activity_fee;
double total_amt;

//Default constructor
cyryxStudent_association ()
{
studentID = "Null";
studentName = "Null";
studentCourse_level = "Null";
studentTitle = "Null";
course_completed_year = 0;
}

//Parameterized Constructor
cyryxStudent_association (String id, String name, String course_level, String title, int ccy)
{
this.studentID = id;
this.studentName = name;
this.studentCourse_level = course_level;
this.studentTitle = title;
this.course_completed_year = ccy;
}

//Getters
public String getStudentID ()
{
return studentID;
}
public String getStudentName ()
{
return studentName;
}
public String getStudentCourse_level ()
{
return studentCourse_level;
}
public String getStudentTitle ()
{
return studentTitle;
}
public int getCourse_completed_year ()
{
return course_completed_year;
}
public double getRegistration_fee ()
{
return registration_fee;
}
public double getActivity_fee ()
{
return findActivity_fee(registration_fee);
}
public double getTotal_amt ()
{
return total_amt(registration_fee, activity_fee);
}

//Setters
public void setStudentID (String id)
{
studentID = id;
}
public void setStudentName (String name)
{
studentName = name;
}
public void setStudentCourse_level (String course_level)
{
studentCourse_level = course_level;
}
public void setStudentTitle (String title)
{
studentTitle = title;
}
public void setCourse_completed_year (int ccy)
{
course_completed_year = ccy;
}

//Find registration fee method
public static double findRegistration_fee (String course_level)
{
if (course_level.equalsIgnoreCase("Certificate"))
{
registration_fee = 75;
}
else if (course_level.equalsIgnoreCase("Diploma"))
{
registration_fee = 100;
}
else if (course_level.equalsIgnoreCase("Degree"))
{
registration_fee = 150;
}
else if (course_level.equalsIgnoreCase("Master"))
{
registration_fee = 200;
}

return registration_fee;
}

//Find activity method
public static double findActivity_fee (double registration_fee)
{
return registration_fee * 0.25;
}

//Find total amount
public static double total_amt (double registration_fee, double activity_fee)
{
return registration_fee + activity_fee;
}

//To string method
public String toString ()
{
return "ID: "+getStudentID()+"\nName: "+getStudentName()+"\nCourse Level:
"+getStudentCourse_level()+"\nTitle: "+getStudentTitle()+"\nCourse Completed Year:
"+getCourse_completed_year()+"\nRegistration Fee: "+getRegistration_fee()+"\nActivity Fee:
"+getActivity_fee()+"\nTotal Amount: "+getTotal_amt ();
}
}

这是我的第二类代码。

import java.util.Scanner;

public class test_cyryxStudent_association {

public static void main (String[] args)
{
Scanner sc = new Scanner (System.in);

int num, i;

System.out.println("Welcome!");
System.out.println("\nEnter the number of students: ");
num = sc.nextInt();
sc.nextLine();

cyryxStudent_association Std[] = new cyryxStudent_association[num];

for (i = 0; i < Std.length; i++)
{
System.out.println("\nEnter ID: ");
Std[i].setStudentID(sc.nextLine());
System.out.println("Enter Name: ");
Std[i].setStudentName(sc.nextLine());
System.out.println("Enter Course Level [Certificate, Diploma, Degree, Master]: ");
Std[i].setStudentCourse_level(sc.nextLine());
System.out.println("Enter Title: ");
Std[i].setStudentTitle(sc.nextLine());

Std[i].getRegistration_fee();
Std[i].getActivity_fee();
Std[i].getTotal_amt();
}

for (i = 0; i < Std.length; i++)
{
System.out.println("\nStudent " + i + 1 + " Information");
System.out.println("===================================");
Std[i].toString();
}


sc.close();
}
}

当 for 循环中的值时,我收到错误。有人能帮我吗?我对编程很陌生,现在学习java已经两个月了。我做错了什么?

这是我的目标。

  1. 创建对象数组并获取用户输入的要操作的对象数量。
  2. 读取并显示对象值数组。

谢谢!

最佳答案

您必须初始化数组的对象。行后:

cyryxStudent_association Std[] = new cyryxStudent_association[num];

执行一个 for 循环,例如:

for(i = 0; i<std.length; i++){
std[i] = new cyryxStudent_association();
}

关于java - 将用户输入返回到 setter 函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62437922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com