我正在为我的编程课程做作业,为草坪护理服务创建一个程序。作为分配的一部分,我必须创建一种方法来对商业企业每1000平方英尺的土地收取5美元的费用,对住宅房屋每1000平方英尺的土地收取6美元的费用,我的问题是如何在我的代码中实施该方法?到目前为止,这是我的代码:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int choice;
do {
System.out.println("Make your choice: ");
System.out.println("1. Commercial");
System.out.println("2. Residential");
System.out.println("3. Done");
choice = in.nextInt();
if (choice!= 1 && choice != 2 && choice != 3)
System.out.println("Incorrect entry, try again!\n");
}while(choice != 1 && choice != 2 && choice != 3);
switch (choice) {
case 1:
commercial();
break;
case 2:
residential();
break;
case 3:
System.out.println("Have a nice day!");
break;
}
}
private static void commercial(){
boolean multi;
Scanner scanner = new Scanner(System.in);
System.out.println("Commercial Customer");
System.out.println("Please enter the customer name: ");
String name = scanner.nextLine();
System.out.println("Please enter the customer phone number: ");
String phone = scanner.nextLine();
System.out.println("Please enter the customer address: ");
String address = scanner.nextLine();
System.out.println("Please enter the square footage of the property: ");
String foot = scanner.nextLine();
Double footage = Double.parseDouble(foot);
System.out.println("Please type true if there is a multi-property discount: ");
String discount = scanner.nextLine();
if (discount.substring(0,1).equals("t") || discount.substring(0,1).equals("T")){
multi = true;
}
else{
multi =false;
}
Commercial cust = new Commercial(name, phone, address, footage, multi);
//cust.calculateCharges();
}
private static void residential(){
boolean senior;
Scanner scanner = new Scanner(System.in);
System.out.println("Residential Customer");
System.out.println("Please enter the customer name: ");
String name = scanner.nextLine();
System.out.println("Please enter the customer phone number: ");
String phone = scanner.nextLine();
System.out.println("Please enter the customer address: ");
String address = scanner.nextLine();
System.out.println("Please enter the square footage of the property: ");
String foot = scanner.nextLine();
Double footage = Double.parseDouble(foot);
System.out.println("Please type true if there is a senior discount: ");
String discount = scanner.nextLine();
if (discount.substring(0,1).equals("t") || discount.substring(0,1).equals("T")) {
senior = true;
}
else{
senior = false;
}
Residential cust = new Residential(name, phone, address, footage, senior);
//cust.calculateCharges();
}
//public static double calculateCharges(){
//double seniordis;
//double multidis;
//}
}
答案 0 :(得分:0)
如果我正确理解了您的问题-为什么不添加:
Double price = (footage * 5.0) / 1000.0;
System.out.println("The price is $" + price);
到商业功能的末尾,与住宅功能的代码相同,但是用6.0代替5.0。
您要在此处执行的操作是将素材乘以每素材成本,例如6/1000,然后将其显示给用户。
答案 1 :(得分:0)
在尝试实现calculateCharge function
之前,您似乎对静态方法和实例方法之间的区别有点困惑。
静态方法属于该类,可以在没有该类实例的情况下调用。另一方面,实例方法属于该类的对象,并且只能在创建该类的对象之后调用。
例如,假设我有一个类Car
和一个实例变量mileage
和两个函数:
public double getMileage() {
return mileage;
}
public static double convertMileageToKm(double mileageInMiles) {
return mileageInMiles * 1.609;
}
如果我想调用getMileage
,则需要创建类Car
的实例。
Car car1 = new Car();
int milage1 = car1.getMilage();
但是,我可以在没有convertMileagetoKm
对象的情况下调用Car
,因为它属于该类
int milageInKm = Car.convertMileageToKm(milage1);
在您的代码中,您正在对类calculateCharges
和Residential
的对象调用静态方法Commercial
。
有几种方法可以解决此问题,我认为这对您退一步并在继续之前考虑如何组织代码将是一个有用的练习。