Java:循环运行总计和错误:缺少return语句

时间:2013-11-11 04:19:31

标签: java swing

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

5 个答案:

答案 0 :(得分:1)

为什么主double的返回类型?

应为public static void main(String[] args)

答案 1 :(得分:1)

public static double main(String[] args)

according to j.l.s 12.1.4:Invoke Test.main

  

要执行的Java main()方法应该是:声明为public,static,   并且无效。它必须指定声明的形式参数(第8.4.1节)   type是String数组。因此,以下任何一种   声明是可以接受的:

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

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


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

这在jls-8.4.7 Method body:

中指定
  

如果声明一个方法具有返回类型,那么编译时   如果方法的主体可以正常完成,则会发生错误。其他   单词,具有返回类型的方法必须仅使用return返回   提供价值回报的陈述;不允许“掉线”   它的结尾“。


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

我没有看到你更新(减少或其他一些操作)productOrdered可能导致它具有值-1。小心一点。我可能希望使用if-else条件进行检查。

答案 2 :(得分: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;

虽然会增加每个循环的总数......

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

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

答案 3 :(得分:0)

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

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

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

if (productOrdered > -1) {

答案 4 :(得分:0)

您的主要方法应该是

public static void main(String[] args){
 //no return values
}