Java - 需要根据用户输入调用三种方法之一

时间:2017-03-27 23:36:35

标签: java methods user-input

我有一个用Java编写医疗计费程序的任务(通过eclipse)。我们需要编写三种计费方法,其中一种是用户选择的。我已经编写了方法,但有两个问题我如何调用用户选择的方法,另外,我在其中一个方法中有一个红色的波浪线,不知道为什么。任何人都可以根据用户输入建议调用相应计费方法的最佳方法吗?

另外,你能告诉我为什么Billing方法2又名“calcBillMed”有一条红色的波浪线?在billAmount下?我是否必须在所有三种方法下声明billAmount?

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("\n\n 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("\t\t  Billing method deductible, amount due = " + method1a);
        }
        else
        {
             total = (billAmount * coveredPercent) - deductible;
            System.out.println("\t\t  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("\t\t  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;

}

}

3 个答案:

答案 0 :(得分:0)

您需要从用户那里获取一些输入。你在那里中途:

module.exports = {
  ...
  resolve: {
    alias: {
      mocha: path.resolve('mocha_patch'),
    }
  },
  ...
};

现在System.out.println("Choose billing type of: deductible, Medicare or co-pay: "); method = scan.nextLine(); 应该包含用户想要调用的方法的名称。您可以通过以下几种方式对此信息采取行动,这是使用method声明的一种方式。

switch

答案 1 :(得分:0)

对于billAmount问题,是的,您必须将billAmount声明为变量或使其成为类字段才能引用和修改它。

如果你想在没有实例化对象的情况下访问它们,你还需要使方法成为静态。

这称为变量范围。在一个方法中创建的变量只能由该单个函数访问。类字段变量可供该类中的所有非静态方法访问。

至于提示用户输入并根据该输入选择方法,最好的方法是运行if / else块或switch语句并提示它们输入整数值

System.out.println("Choose billing type:\n1. Deductible\n2. Medicare\n3. 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;
    }
}

答案 2 :(得分:0)

首先将方法签名更改为公共静态,保持返回值为double。

public static double calcBillDeduct (double billAmount, double method1a, double method1b) 
{
   ....
}

public static double calcBillMed (double medicare)
{
   ....
}

public static double calcCoPay (double coPay)      
{
   ....
}

接下来你需要声明变量double billAmount;如果您打算在函数中使用它而不通过参数传递给它们,则在main方法之外。

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:\ndeduct\nmedicare\nco-pay\nexit");
        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"));