如何在满足JAVA条件时重启程序?

时间:2016-08-04 23:02:12

标签: java

我是JAVA的新手,正在编写一个JAVA代码,用于计算患者的费用。 如果用户喜欢计算另一名患者的费用,他会进入" Y"在提示符上,程序需要再次计算。

如何调用main()让程序重新运行?

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package demo;
import java.util.Scanner;
/**
 *
 * @author seshasai
 */
public class PatientHospital {

    public static void main(String[] args){
      double totalCost = 0;

      totalCost = costCalculator();
      System.out.println("totalCosts " + totalCost);
      tryAgain();


    }

    public static void tryAgain(){
         String tryAgain;
      System.out.println("Do you want to calculate cost for another patient (Y/N): ");
      Scanner in = new Scanner (System.in);
      tryAgain = in.next();
      if(tryAgain.equalsIgnoreCase("Y")){
        main();

      }

    }

    public static double costCalculator(){
        System.out.println("Enter YES if it is a day charge, NO if it is night charge");
        Scanner i = new Scanner (System.in);
        double totalCost =0;
        String day = i.next();;

        if (day.equalsIgnoreCase("YES")){
             totalCost = dayCharges();


        }else if (day.equalsIgnoreCase("NO")){
            totalCost = overNightCharges();

        }
       return totalCost;

    }
    public static double overNightCharges(){
        Scanner i = new Scanner (System.in);
        System.out.println("Enter overnight, medication and labcosts");
        double onCharges = i.nextDouble();
        double medication = i.nextDouble();
        double lab = i.nextDouble();

        double totalFees = onCharges + medication + lab;
        return totalFees;
    }

    public static double dayCharges(){
        Scanner i = new Scanner (System.in);
        System.out.println("Enter medication and labcosts");

        double medication = i.nextDouble();
        double lab = i.nextDouble();

        double totalFees = medication + lab;
        return totalFees;
    }

}

0 个答案:

没有答案