我有一个任务是用Java编写医疗计费程序(通过eclipse(。我们需要编写三种计费方式,其中一种由用户选择。我已经编写了方法,但是有两个问题如何调用用户选择的方法,另外,我在其中一个方法中有一条红色波浪线,不知道为什么。任何人都可以根据用户输入建议调用相应计费方法的最佳方式吗?
另外,你能告诉我为什么计费方式 2 又名"calcBillMed"有一条红色波浪线吗? 在账单金额下?我是否必须同时申报账单金额?
import java.util.Scanner;公共类 PatientBillingV2 {
//import java.text.NumberFormat;import javax.swing.JOptionPane;
//Project requirements
//Loop until the user enters -1 for the patient last name
//Request patient last name, first name, claim amount and billing method, 1,2,3
public static void main(String[] args) {
double billAmount;
String method;
Scanner scan = new Scanner(System.in);
displayTitle();
adjustName(null);
System.out.print("nn Enter claim amount: ");
billAmount = scan.nextDouble();
System.out.println("Choose billing type of: deductible, Medicare or co-pay: ");
method =scan.nextLine();
>>>>>>>>>>>>>I want the code to go to the correct method based on user input starting here<<<<<<<<<<<<<<<<<<<<<<<
}
//-------------------------------------------------------------
// Displays program title
//-------------------------------------------------------------
public static void displayTitle()
{
System.out.println("Patient Billing");
}
//------------------------------------------------------------
//Formats patients last name and first name, converting the first character of each to upper case
//------------------------------------------------------------
public static void adjustName(String in)
{
String lastname, firstname, method;
Scanner scan = new Scanner (System.in);
System.out.println("Enter patients last name: ");
lastname = scan.nextLine();
System.out.println(lastname.substring(0, 1).toUpperCase() + lastname.substring(1));
System.out.println("Enter paitents fist name: ");
firstname = scan.nextLine();
System.out.println(firstname.substring(0, 1).toUpperCase() + firstname.substring(1));
}
//------------------------------------------------------------
//Billing method 1, coverage percent = 80 with a $50 deductible, unless bill is less than $50
//-----------------------------------------------------------
public double calcBillDeduct (double billAmount, double method1a, double method1b)
{
final double coveredPercent = .80;
double deductible = 50.00;
double total;
//Scanner scan = new Scanner(System.in);
if (billAmount > deductible)
{
total = (billAmount + deductible) * coveredPercent;
System.out.println("tt Billing method deductible, amount due = " + method1a);
}
else
{
total = (billAmount * coveredPercent) - deductible;
System.out.println("tt Billing method deductible, claim amount is less than the deductible = " + method1b);
}
return total;
}
//-----------------------------------------------------------------------
// Billing method2 - Medicare - patient is billed 35% of claim amount
//-----------------------------------------------------------------------
public double calcBillMed (double medicare)
{
final double medicadePercent = .35;
medicare = billAmount * medicadePercent;
System.out.println("tt Billing method Medicare, amount due = " + medicare);
}
//-------------------------------------------------------------------------
//Billing method3 is co-pay amount of $15 regardless of claim amount
//-------------------------------------------------------------------------
public double calcCoPay (double coPay)
{
final double copay = 35;
}
}
您需要从用户那里获取一些输入。你已经完成了一半:
System.out.println("Choose billing type of: deductible, Medicare or co-pay: ");
method = scan.nextLine();
现在method
应包含用户想要调用的方法的名称。有几种方法可以对这些信息进行操作,以下是使用 switch
语句的一种方法。
boolean valid = isValidMethod(method); // make a method that ensures that the user entered something sane. Check if it's null, check if it corresponds to a valid method name, etc.
if(!valid) {
// user entered an invalid method, uh oh! ask them for valid input
// I leave this to you
}
// it's valid, switch on the method name and call the appropriate method
switch(method) {
case "calcBillDeduct":
calcBillDeduct(billAmount, method1a, method1b);
break;
case "calcBillMed":
calcBillMed(billAmount);
break;
// etc
default:
// good to include a default, but it's an invalid method in this case
throw new IllegalArgumentException("Invalid method: " + method);
}
对于 billAmount 问题,是的,您必须将 billAmount 声明为变量或使其成为类字段才能引用和修改它。
此外,如果您想在没有实例化对象的情况下访问它们,则需要将方法设为静态。
这称为变量作用域。 在一个方法中创建的变量只能由该单个函数访问。 类字段变量可供该类中的所有非静态方法访问。
至于提示用户输入并根据该输入选择方法,最好的方法是运行 if/else 块或 switch 语句并提示他们输入整数值
System.out.println("Choose billing type:n1. Deductiblen2. Medicaren3. co-pay");
int selection = scan.nextInt();
boolean validSelection = false;
while(!validSelection) {
switch (selection) {
case 1:
calcBillDeduct(billAmount, method1a, method1b);
validSelection = true;
break;
case 2:
calcBillMed(billAmount);
validSelection = true;
break;
case 3:
calcCoPay(coPay);
validSelection = true;
break;
default:
break;
}
}
首先将方法签名更改为公共静态,保持返回值双精度。
public static double calcBillDeduct (double billAmount, double method1a, double method1b)
{
....
}
public static double calcBillMed (double medicare)
{
....
}
public static double calcCoPay (double coPay)
{
....
}
接下来,您需要在主方法之外声明变量 double billAmount; 如果您打算在函数中使用它而不通过参数将其传递给它们。
public static double billAmount;
public static void main(String[] args)
{
....
}
这会将变量置于其他方法的范围内,并且它必须是静态的,因为您正在调用 main 中的所有内容,这是静态的。
最后,这里有一个简单的做/虽然,要求选择 - 扣除,医疗保险或共付额。您所要做的就是用方法调用填写 if。
Scanner scan = new Scanner(System.in);
String input;
do {
System.out.println("Choose from the following options:ndeductnmedicarenco-paynexit");
input = scan.nextLine();
if(input.equals("deduct"))
{
}
else if(input.equals("medicare"))
{
}
else if(input.equals("co-pay"))
{
}
else if(!input.equals("exit")) {
System.out.println("Error with input : " + input);
}
} while (!input.equals("exit"));