我还是 Java 的新手,我不知道如何让它保持运行总数。任何关于我应该查找的建议都会很棒。感谢您的时间。
import javax.swing.*;
public class OrderingProducts2
{
public static double main(String[] args)
{
// Number of products
final int NUMBER_OF_PRODUCTS = 5;
//all of the valid values
int[] validValues = {1, 2, 3, 4, 5};
//prices that coralate with the values
double[] prices = {2.98, 4.50, 9.98, 4.49, 6.87};
//Starting total price of order
double total = 0.00;
//Sring for chosing a product to order
String strProducts;
int productOrdered;
//Starting price of unnamed product
double productPrice = 0.00;
//boolean used to validate product
boolean validProduct = false;
//User input of product number
strProducts = JOptionPane.showInputDialog(null,"Enter the product number you want to order or done");
//product number being converted to an integer
productOrdered = Integer.parseInt(strProducts);
//string for getting the quanity
while(productOrdered > -1)
{
String strQuanity;
int productQuanity;
//user input of quanity
strQuanity = JOptionPane.showInputDialog(null,"Enter the amount of product " + strProducts);
//conversion of the quanity to an integer
productQuanity = Integer.parseInt(strQuanity);
//validation and totaling of the price of order
for(int x = 0; x < NUMBER_OF_PRODUCTS; ++x)
{
if(productOrdered == validValues[x])
{
validProduct = true;
productPrice = prices[x];
total = productPrice * productQuanity;
}
}
}
//if there awas a valid out put this message will show
if (validProduct)
JOptionPane.showMessageDialog(null, "The price for product " + productOrdered + " is $" + productPrice + ". The price for your ordered item/items so far is $" + total);
//if output was not valid this message will show
else
JOptionPane.showMessageDialog(null,"Sorry1 - invalid product entered");