绝对的java初学者。我不能使用返回的变量

时间:2018-02-18 04:32:30

标签: java

这项任务是计算医院就诊的费用。我试图询问用户“夜间充电”,“药物充电”和“labCharge”的价格是多少。然后我尝试使用输入将它们添加到名为“total”的方法中。接下来,我尝试通过键入System.out.println("Your total charge is: " + total(totalCost)从main方法中的“total”方法打印结果/返回变量。我以为total(totalCost)会检索“total”方法返回的变量。

package hospitalstay;
import java.util.Scanner;
/* total charges
if overnight charges
medication charges
lab charges
ask user if new patient*/

public class HospitalStay {


public static void main(String[] args) {
    System.out.println("Your total charge is:   " + total(totalCost); // i want to print the "totalCost variable" returned by the "total" method.


}

public static double overnightCharge () {// asking user for overnight charge
    Scanner sc = new Scanner (System.in);
    System.out.println("What is your overnight charge");
    double overnightCharge;
    overnightCharge = sc.nextDouble();
    return overnightCharge;
}

public static double medicationCharge() {// asking user for medication charge
    Scanner sc = new Scanner (System.in);
    System.out.println("What is your medication charge");
    double medicationCharge;
    medicationCharge = sc.nextDouble();
    return medicationCharge;
}

public static double labCharge() {//asking user for lab charge
    Scanner sc = new Scanner (System.in);
    System.out.println("What is your lab charge");
    double labCharge;
    labCharge = sc.nextDouble();
    return labCharge;
}
public static double total (double medicineCharge, double labCharge, double overnightCharge) {
    double totalCost;
    if (overnightCharge == 0) {
        totalCost = (overnightCharge + medicineCharge + labCharge); //Calculating all three charges
    }
    else {
        totalCost = (medicineCharge + labCharge);
    }
    return totalCost;
}
}

4 个答案:

答案 0 :(得分:0)

您将错误的输入传递给total,将main更改为

public static void main(String[] args) {
    System.out.println("Your total charge is:   " + total(medicationCharge(), labCharge(), overnightCharge()));
}

此外,在您的total方法中,您不需要if条件,因此您可以将其简化为:

public static double total(double medicineCharge, double labCharge, double overnightCharge) {
    return  (overnightCharge + medicineCharge + labCharge);
}

答案 1 :(得分:0)

首先,您已为方法“total”定义了三个参数,但是您只在主方法中指定了一个参数:

total(totalCost)

为了最大限度地减少代码中需要更改的内容,我只需将total()方法更改为:

public static double total() {
    double totalCost = overnightCharge();
    totalCost += medicationCharge();
    totalCost += labCharge();
    return totalCost;
}

并在您的主要方法中:

public static void main(String[] args) {
    System.out.println("Your total charge is:   " + total();
}

答案 2 :(得分:0)

您已将总方法更改为

public static double total () {
        return overnightCharge() + medicineCharge() + labCharge();
    }

还将主要方法更改为

public static void main(String[] args) {
    System.out.println("Your total charge is:   " + total());
}

答案 3 :(得分:0)

“解释为什么您的代码无法按预期运行:”

  • 您已经创建了一个功能 total(),其中需要3个参数,即(double medicineCharge,double labCharge,double overnightCharge)

    public static double total (double medicineCharge, double labCharge, double overnightCharge) {
    
            return totalCost;
        }
    
  

但是当您在 main()中调用此函数时,您只传递一个总数为 totalCost 的参数。

public static void main(String[] args) {
    System.out.println("Your total charge is:   " + total(totalCost); // i want to print the "totalCost variable" returned by the "total" method.


}
  • 您还犯了一个拼写错误“medicineCharge”

您可以尝试这样的方法来实现所需的输出:

import java.util.Scanner;

public class Hospital {
    static Scanner input;
    private static double overnightCharge, medicationCharge, labCharge,
            totalCost;

    public static double takeInput() { // single function to take input
        input = new Scanner(System.in);
        return input.nextDouble();
    }

    public static double overnightCharge() {// asking user for overnight charge

        System.out.println("What is your overnight charge");
        overnightCharge = takeInput();
        return overnightCharge;
    }

    public static double medicationCharge() {// asking user for medication
        // charge

        System.out.println("What is your medication charge");
        medicationCharge = takeInput();

        return medicationCharge;
    }

    public static double labCharge() {// asking user for lab charge

        System.out.println("What is your lab charge");
        labCharge = takeInput();

        return labCharge;
    }

    public static double total() {
        overnightCharge();
        medicationCharge();
        labCharge();

        if (overnightCharge == 0) {
            totalCost = (overnightCharge + medicationCharge + labCharge); // Calculating
                                                                            // all
                                                                            // three
                                                                            // charges only when overnightCharge = 0  
        } else {
            totalCost = (medicationCharge + labCharge);
        }
        return totalCost;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Total :" + total());
    }

}
  

注意:我还减少了再次调用扫描程序的代码。