我的程序必须询问电池的数量并显示消息上的数量。为此,我必须传递变量quantity = getQuantity,以便我可以显示所选的电池数量。问题是,当我这样做时,它要求用户输入"您想要购买多少电池"两次。我正在学习和学生,所以请提供帮助,以提供更好的理解。我不想简单地修改我想要理解的代码。我只是添加数量和其他几个选项,但似乎没有显示所选电池的数量,只询问一次问题。
import javax.swing.JOptionPane;
import java.io.*;
/**
* @author Arnie
*/
public class VapeSolutions2 {
/**
* @param args
*/
public static void main(String[] args) {
// declare variables
String openingMsg, nameInputMsg, customerName, nameOutputMsg,
returnInputMsg, customerReturn, returnOutputMsg,
greetingOutputMsg, outputMsg, colorSelection, colorInputMsg, ColorOutputMsg, priceOutputMsg,
batteryOutputMsg;
int quantity;
double grandTotal;
// display opening message
openingMsg = "*** Welcome to Vape Ordering Solutions ***\n"
+ " It's a great day to order a Vape Supplies!";
JOptionPane.showMessageDialog(null, openingMsg);
// get required input using dialogs
nameInputMsg = "Please enter your name: ";
customerName = getStringInput(nameInputMsg);
returnInputMsg = "Are you a returning customer (yes or no)? ";
customerReturn = getStringInput(returnInputMsg);
colorInputMsg = "What Color would you like?";
colorSelection = getStringInput(colorInputMsg);
grandTotal = totalCost();
quantity = getQuantity();
// build output strings
nameOutputMsg = "Welcome " + customerName + ".\n\n";
returnOutputMsg = "Your return customer status is " + customerReturn + ".\n";
greetingOutputMsg = "Thank you for ordering from Vape Ordering Solutions!" + "\n\n"
+ "Your order will be shipped the following day" + ".\n";
ColorOutputMsg = "Your Color selected is " + colorSelection + ".\n";
batteryOutputMsg = "Total Batteries Ordered is" + quantity + ".\n";
priceOutputMsg = "Your total purchase price is $" + grandTotal + "\n";
// create and display output string
outputMsg = nameOutputMsg + returnOutputMsg + greetingOutputMsg + ColorOutputMsg + batteryOutputMsg
+ priceOutputMsg;
JOptionPane.showMessageDialog(null, outputMsg);
System.exit(0);
} // end main()
private static String getStringInput(String prompt) {
String input;
input= JOptionPane.showInputDialog(prompt);
int i = 0;
while (input.length() == 0 && i < 3){
JOptionPane.showInputDialog("Please enter a valid value\n"+ prompt);
i++;
if (i == 3){
JOptionPane.showMessageDialog(null,"You have exceeded the maximum attempts to input correct data");
System.exit(0);
}
}
return input;
}
private static int getQuantity( ){
int quantity;
String quantityMsg = "How many batteries would you like to order?";
String quant = getStringInput (quantityMsg);
quantity = Integer.parseInt(quant);
return quantity;
}
// total = grandTotal (quantity, 5,.07)
private static double totalCost( ) {
int number;
double cost, salesTaxRate, tax, grandTotal, subTotal;
cost = (20.00);
number = getQuantity ();
salesTaxRate = (.07);
subTotal = number * cost;
tax = subTotal * salesTaxRate;
grandTotal = subTotal + tax;
return grandTotal;
}
} // end class VapeSolutions2
答案 0 :(得分:0)
尝试:
quantity = getQuantity();
grandTotal = totalCost(quantity);
并更新您的方法&#34; totalCost&#34;:
// total = grandTotal (quantity, 5,.07)
private static double totalCost( int quantity) {
//code ...
number = quantity;
//code ...
}