gpt4 book ai didi

Java:循环总计和错误:缺少返回语句

转载 作者:行者123 更新时间:2023-11-30 08:28:52 24 4
gpt4 key购买 nike

我对 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");

最佳答案

为什么main的返回类型double

应该是public static void main(String[] args)

关于Java:循环总计和错误:缺少返回语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19898749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com