我正在尝试为学校编写一个简单的Java程序,它执行以下操作:
健身房会员资格: "编写将输入客户年龄和月数的代码,并打印每月费率和总费用。提示用户输入相应的输入,并显示有意义的输出消息。确保月份大于0且年龄大于0。"
我的问题是:
custMonths
int总是返回0,我不知道为什么。 selectAge
方法开头的方法。 这是我的Java代码:
import java.util.*;
public class GymMembership {
public static void main (String[] args) {
//create test customer (customer0) via the constructor
GymMembership customer0 = new GymMembership(70, 12);
customer0.selectAge(customer0.custAge);
customer0.printCustomer();
customer0.getMonthlyRate(customer0.ageNamed);
//prompt user for two integer inputs to create customer1
Scanner scnr = new Scanner(System.in);
System.out.println("Enter customer age, then number of months for contract: ");
GymMembership customer1 = new GymMembership(scnr.nextInt(), scnr.nextInt());
customer1.selectAge(customer1.custAge);
customer1.printCustomer();
}
//the constructor
GymMembership(int custAge, int custMonths) {
this.custAge = custAge;
this.custMonths = custMonths;
}
//instance variables
private int custAge;
private int custMonths;
int monthlyRate;
int childRate = 15;
int adultRate = 25;
int seniorRate = 20;
String ageNamed;
public String selectAge(int custAge) {
Scanner x = new Scanner(System.in);
int age = custAge;
ageNamed = "badInput";
do {
if (age >= 1 && age <= 18) {
ageNamed = "child";
}
else if (age >= 19 && age <= 64) {
ageNamed = "adult";
}
else if (age > 64 && age <= 120) {
ageNamed = "senior";
}
else {
ageNamed = "badInput";
System.out.println("Age must be a positive number between 1 and 120.");
break;
}
} while(ageNamed.equals("badInput"));
return ageNamed;
}
public int getMonthlyRate(String ageNamed) {
if (ageNamed.equalsIgnoreCase("child")) {
monthlyRate = 15;
} else if (ageNamed.equalsIgnoreCase("adult")) {
monthlyRate = 25;
} else {
monthlyRate = 20;
}
return monthlyRate;
}
public void printCustomer() {
if (ageNamed.equals("badInput") != true) {
System.out.println("The customer is a/an " + ageNamed + " and is " + custAge + " years old.");
System.out.println("The customer is signed up for a " + custMonths + " month contract.");
System.out.println("The monthly rate is " + monthlyRate);
}
else {
selectAge(customer1.custAge); //this is broken since I cannot access customer1 object from this "printCustomer" method.
}
}
}
我意识到我很可能在这里犯了一些不好的初学者错误,但我不太确定它们是什么。
答案 0 :(得分:3)
你似乎过于复杂化了。这里的一些问题包括
selectAge(customer1.custAge);
无效或。您在GymMembership类中,您可以直接看到当前对象的年龄字段,然后使用它。getMonthlyRate(...)
方法很危险,因为它要求计算单独的不必要字段并作为String传入,并且此字符串完全没必要,这可能会导致您出现问题。该类已经知道通过年龄和月份字段计算费率所需的信息 - 删除String参数并使用该类自己的字段。public class GymMemb2 {
private int age;
private int months;
public GymMemb2(int age, int months) {
this.age = age;
this.months = months;
}
public int getAge() {
return age;
}
public int getMonths() {
return months;
}
public double getMonthlyRate() {
// calculations using age and months for monthly rate
// return result
}
public String print() {
String text = "age: " + age + ", months: " + months;
// also add monthly rate info
return text;
}
}
然后在main方法中,再次创建new Scanner(System.in)
,只执行一次,并使用它来获取输入。在main中你将使用while循环来保持循环,直到输入有效输入,然后创建你的健身房成员资格对象。我认为GymMembership类不应该有任何println,而是返回可以由main打印的字符串。
同样在main的末尾,通过调用它上面的.close()
来关闭扫描仪。这应该只在程序完全完成获取用户输入时完成,因为一旦关闭它可能不会重新打开。