我是新來的方法,並試圖找到一種方法,從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;
}
}
非常感謝總工作現在我不是編程很大,但我通常會爲期不遠了那個年齡現在 – user3017497
不客氣。如果一個答案能幫助你,那麼在S.O中表達感謝的最好方式就是接受和/或提高答案的答案http://meta.stackoverflow.com/help/someone-answers – trogdor