2

我还是 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");
4

5 回答 5

1

为什么 main 的返回类型是double

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

于 2013-11-11T04:21:45.050 回答
1
public static double main(String[] args)

根据jls 12.1.4:Invoke Test.main

Java 的main()执行方法应该是:声明为 public、static 和 void。它必须指定一个声明类型为字符串数组的形式参数(第 8.4.1 节)。因此,可以接受以下任一声明:

所以要么:public static void main(String[] args)

或者,public static void main(String... args)


同样,假设您正在使用public static double main()方法,您必须返回双精度类型值:

这是在jls-8.4.7 方法体中指定的:

如果将方法声明为具有返回类型,则如果方法的主体可以正常完成,则会发生编译时错误。换句话说,具有返回类型的方法必须仅通过使用提供值返回的 return 语句返回;不允许它“掉下它的身体末端”。


while(productOrdered > -1)
{
  // your code
}

我没有看到您已更新(递减或其他操作)productOrdered这可能导致它具有 value -1。小心它。我可能希望if-else有条件地检查它。

于 2013-11-11T04:24:42.830 回答
1

有两个直接的问题,但很难知道哪个是错误的......(或不太正确)......

public static double main(String[] args)

由于两个原因之一是错误的。如果这是假设您是应用程序的主要入口点,那么它应该是……

public static void main(String[] args) 

否则 Java 将无法运行您的应用程序。

如果不是(并且它只是static您想要运行的某种方法),那么您应该return在它存在之前有一个声明。public static double main(String[] args)在这种情况下,除非您提供某个地方的实现来调用它,否则您不太可能运行该类......

例如...

public static void main(String[] args) {
    OrderingProducts2.main(args);
}

更新

为了生成运行计数,您需要从循环中提供某种用户能够输入的退出值,在您使用的代码中,但您永远不会在循环中productOrdered更改该值。while

更好的解决方案可能是productOrdered在进入循环之前验证 并productQuanity用作退出值。

在你的主循环中,你也没有增加总数,但你只是设置它的值......

total = productPrice * productQuanity;

相反,您可以使用类似...

total += productPrice * productQuanity;

While 将增加每个循环的总数...

最后,一个可运行的例子......

// 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);

for (int x = 0; x < NUMBER_OF_PRODUCTS; ++x) {
    if (productOrdered == validValues[x]) {
        validProduct = true;
        productPrice = prices[x];
    }
}

if (validProduct) {

    int productQuanity = -1;
    do {

        //string for getting the quanity 
        String strQuanity;

        //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    

        if (productQuanity > -1) {
            for (int x = 0; x < NUMBER_OF_PRODUCTS; ++x) {

                if (productOrdered == validValues[x]) {
                    validProduct = true;
                    productPrice = prices[x];
                    total += productPrice * productQuanity;
                }

            }

        }

    } while (productQuanity > -1);

    JOptionPane.showMessageDialog(null, "The price for product " + productOrdered + " is $" + productPrice + ". The price for your ordered item/items so far is $" + total);

} else {
    JOptionPane.showMessageDialog(null, "Sorry1 - invalid product entered");
}
于 2013-11-11T04:34:24.000 回答
0

main 方法应具有以下签名,否则您将没有 Java 应用程序的入口点。

public static void main(String[] args) // return type is void and not double

而且,你不需要在那里等待一段时间,它只是一个无限循环。将其替换为if.

if (productOrdered > -1) {
于 2013-11-11T04:20:58.780 回答
0

你的主要方法应该是

public static void main(String[] args){
 //no return values
}
于 2013-11-11T04:28:33.907 回答