2014-03-05 52 views
0

我是新来的方法,并试图找到一种方法,从calculateTotalStay方法和calculateMiscCharges方法,并将它们加在一起在calculateTotalCharges方法,我无法弄清楚如何重用值计算我尝试过的事情,包括气质变量=方法名加载,但引起第一种方法来显示超过一次,任何帮助赞赏解决方法计算值

import java.util.Scanner; 

public class Wk6Lab1Ex1 { 

public static double standardCharge; 
public static double miscCharges; 
public static double totalCharges=standardCharge+miscCharges; 

public static void main(String[] args) { 
    int patientCount = 1; 

    calculateTotalCharges(); 

} 


/** 
* A method to calculate to read in the number of days spent in the clinic and return the figure calculated 
* @return 
*/ 
public static double calculateStayCharges() { 
    String message = "Please enter the number of days you spent in the clinic"; 
    int daysInClinic=readPositiveInt(message); 

    double standardCharge=300*daysInClinic; 

    System.out.println("Your standard charges are" + " " + standardCharge); 
    return standardCharge; 


} 
/** 
* A method to read in and calculate all the miscellaneous charges and total them 
* @return the total for all misc fees 
*/ 
public static double calculateMiscCharges() { 

    String message = "Please enter your medication charges"; 
    double medicationCharges=readPositiveDouble(message); 

    String message1 = "Please enter your surgical charges"; 
    double surgicalCharges=readPositiveDouble(message1); 

    String message2 = "Please enter your physical rehabilitation charges"; 
    double physicalRehabiliation=readPositiveDouble(message2); 

    String message3 = "Please enter your lab fees"; 
    double labFees=readPositiveDouble(message3); 

    double miscCharges=medicationCharges+surgicalCharges+labFees+physicalRehabiliation; 

    System.out.println("Your miscellaneous charges are" + " " + miscCharges); 


    return miscCharges; 


} 

/** 
* A method to take the stay charges and misc charges and total them 
* @return the total treatment cost 
*/ 
public static double calculateTotalCharges() { 

    calculateStayCharges(); 

    calculateMiscCharges(); 



    int standardCharge = 0; 
    int miscCharges = 0; 
    double totalCharges=standardCharge+miscCharges; 

    System.out.println("Your total charges are" + " " + totalCharges); 

    return totalCharges; 


} 
/** 
* Ensuring the number entered for amount of days is an integer 
* @param prompt the enter number of days prompt 
* @return the int value for number of days 
*/ 

public static int readInt(String prompt) 
{ 
    // Create a scanner object for input from the console window 
    Scanner keyboard = new Scanner(System.in); 

    // boolean flag which is used to control the 
    // data validation loop 

    boolean numGood = false; // Assume the worst 
    do 
    { 
     System.out.print(prompt); // ask for the value 
     if(!keyboard.hasNextInt()) // check if what's in the keyboard buffer is not a double 
     { 
      System.out.println("You must enter an value!"); // display an error message 
      keyboard.nextLine(); // consume the bad value entered 
     } 
     else 
      numGood = true; // value entered is good 
    } while(!numGood); 
    // at this point we know the value in the 
    // keyboard buffer is numeric so we can go ahead and 
    // return it. 
    return keyboard.nextInt(); 
} 
/** 
* The readPositiveInt method reads a positive int value 
* from the console window and will display an appropriate 
* error message if a non-positive int value is entered. 
* @param prompt A prompt to request the user to enter a value 
* @return The positive int value entered. 
*/ 
public static int readPositiveInt(String prompt) 
{ 
    int value; 
    do 
    { 
     value = readInt(prompt); // ask for and read an double value 
     if (value <0) // check if the value entered is less than 0 
      // display an error message 
      System.out.println("Error - you must enter a positive numeric value greater than zero!"); 
    } while (value <0); 
    // at this point we know the value entered is positive 
    // so return it 
    return value; 
} 
/** 
* readDouble ensures the values entered for fees are doubles 
* @param prompt asks user to enter various fees 
* @return returns the double value 
*/ 
public static double readDouble(String prompt) 
{ 
    // Create a scanner object for input from the console window 
    Scanner keyboard = new Scanner(System.in); 

    // boolean flag which is used to control the 
    // data validation loop 

    boolean numGood = false; // Assume the worst 
    do 
    { 
     System.out.print(prompt); // ask for the value 
     if(!keyboard.hasNextDouble()) // check if what's in the keyboard buffer is not a double 
     { 
      System.out.println("You must enter an value!"); // display an error message 
      keyboard.nextLine(); // consume the bad value entered 
     } 
     else 
      numGood = true; // value entered is good 
    } while(!numGood); 
    // at this point we know the value in the 
    // keyboard buffer is numeric so we can go ahead and 
    // return it. 
    return keyboard.nextDouble(); 
} 
/** 
* The readPositiveDouble method reads a positive double value 
* from the console window and will display an appropriate 
* error message if a non-positive double value is entered. 
* @param prompt A prompt to request the user to enter a value 
* @return The positive double value entered. 
*/ 
public static double readPositiveDouble(String prompt) 
{ 
    double value; 
    do 
    { 
     value = readDouble(prompt); // ask for and read an double value 
     if (value <0) // check if the value entered is less than 0 
      // display an error message 
      System.out.println("Error - you must enter a positive numeric value!"); 
    } while (value <0); 
    // at this point we know the value entered is positive 
    // so return it 
    return value; 
} 
} 

回答

0

可以对方法的返回值赋值给一个变量,但要确保您只调用一次方法:

public static double calculateTotalCharges() { 
    double standardCharge = calculateStayCharges(); 
    double miscCharges = calculateMiscCharges(); 
    double totalCharges=standardCharge+miscCharges; 
    System.out.println("Your total charges are" + " " + totalCharges); 
    return totalCharges; 
} 

此外,我将变量standardChargemiscCharges的类型更改为double以匹配方法的返回类型。我认为这将符合您的预期结果。

+0

非常感谢总工作现在我不是编程很大,但我通常会为期不远了那个年龄现在 – user3017497

+0

不客气。如果一个答案能帮助你,那么在S.O中表达感谢的最好方式就是接受和/或提高答案的答案http://meta.stackoverflow.com/help/someone-answers – trogdor

0

调用返回值的方法,就像它是一个void方法一样,将导致方法返回的值丢失。通过使用表达式中的值并分配它们将被保留。试试这个:

/** 
* A method to take the stay charges and misc charges and total them 
* @return the total treatment cost 
*/ 
public static double calculateTotalCharges() { 

    int standardCharge = 0; 
    int miscCharges = 0; 
    double totalCharges=calculateStayCharges()+calculateMiscCharges(); 

    System.out.println("Your total charges are" + " " + totalCharges); 

    return totalCharges; 


}