亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在我的代碼中輸入“為 x 平方英尺的數量收取 x 金額”?

如何在我的代碼中輸入“為 x 平方英尺的數量收取 x 金額”?

呼啦一陣風 2021-11-17 16:53:45
我正在為我的編程課做作業,為草坪護理服務創建一個程序。對于任務的一部分,我必須創建一種方法,對商業企業每 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();}
查看完整描述

2 回答

?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

如果我正確理解您的問題-為什么不添加:


Double price = (footage * 5.0) / 1000.0;

System.out.println("The price is $" + price);

到商業功能的末尾,與住宅功能相同的代碼,但將 5.0 替換為 6.0。


您在這里要做的是將素材乘以每個素材的成本,例如 6/1000,然后將其顯示給用戶。


查看完整回答
反對 回復 2021-11-17
?
人到中年有點甜

TA貢獻1895條經驗 獲得超7個贊

在您嘗試實現 之前,您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.


有幾種方法可以解決這個問題,我認為在繼續之前退后一步考慮如何組織代碼對您來說是一個有用的練習。


查看完整回答
反對 回復 2021-11-17
  • 2 回答
  • 0 關注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號