While-Loop完成问题和最终值计算

时间:2013-10-01 20:45:26

标签: java javascript loops while-loop

我的循环语句出现问题,如果没有恢复原来的循环条件,就无法正常执行。另外,如果我希望我的最终输出包含所购买产品总数的定价值,我将如何实现这一目标?

所需的最终产品:客户购买所有9件商品

  

请输入您的姓名:John Smith

     

GRAPEFRUIT PRODUCT

     
      
  1. gPod shuffle $ 49
  2.   
  3. gPod Touch $ 299
  4.   
  5. gPad Mini $ 329
  6.   
  7. gPad 2 $ 399
  8.   
  9. gPhone $ 199
  10.   
  11. gMac $ 1299
  12.   
  13. MacNovel Pro $ 1199
  14.   
  15. MacNovel Air $ 999
  16.   
  17. MiniMac $ 599
  18.   
  19. 完成我的订单
  20.         

    请从上面的菜单中选择一个项目:5

         

    请从上面的菜单中选择其他项目:2

         

    请从上面的菜单中选择其他项目:7

         

    请从上面的菜单中选择其他项目:9

         

    请从上面的菜单中选择其他项目:3

         

    请从上面的菜单中选择其他项目:4

         

    请从上面的菜单中选择其他项目:6

         

    请从上面的菜单中选择其他项目:1

         

    请从上面的菜单中选择其他项目:8

         

    请从上面的菜单中选择其他项目:10谢谢   与Grapefruit公司订购,John Smith

         

    订购的商品总数:9

         

    订购商品的价格:$ 5371

         

    销售税:$ 349.115

         

    应付总额:$ 5720.115

这是我的代码:

public static void main(String[] args) {

        // Declare Variables
        Scanner input = new Scanner (System.in);
        String CustomerName;
        int gpodShuffle = 1;
        int gpodTouch = 2;
        int gpadMini = 3; 
        int gpadTwo = 4;
        int gphone = 5;
        int gmac = 6;
        int macnovelPro = 7;
        int macnovelAir = 8;
        int miniMac = 9;
        int nNumber = 0;
        int nProducts = 0;
        int nTotal = 0;

        //Declare Constants 
        final int SENTINEL = 10;
        final double SALES_TAX = 6.5;
        final int GPOD_SHUFFLE = 49;
        final int GPOD_TOUCH = 299;
        final int GPAD_MINI = 329;
        final int GPAD_TWO = 399;
        final int GPHONE = 199;
        final int GMAC = 1299;
        final int MAC_NOVELPRO = 1199;
        final int MAC_NOVELAIR = 999;
        final int MINI_MAC = 599;

        //Prompt user to enter name
        System.out.println("Please enter your name: ");

        //Enter user name
        CustomerName = input.nextLine();

        //Print Blank Line 
        System.out.println("");

        //Begin Product Listing
        System.out.println("GRAPEFRUIT PRODUCT:");

        System.out.println("1. gPod shuffle $49");

        System.out.println("2. gPod Touch   $299");

        System.out.println("3. gPad Mini    $329");

        System.out.println("4. gPad 2       $399");

        System.out.println("5. gPhone       $199");

        System.out.println("6. gMac         $1299");

        System.out.println("7. MacNovel Pro $1199");

        System.out.println("8. MacNovel Air $999");

        System.out.println("9. MiniMac      $599");

        System.out.println("10. Complete my order");

        //Keep reading until the input is 10
        while (nNumber != SENTINEL) {
            //Calculate entered items
            nTotal = nTotal + nNumber;

            nProducts++;

        System.out.println("\nPlease select an item from the menu above: ");

        //Read number entered by the user
        nNumber = input.nextInt();

        if (nNumber == SENTINEL) 
        System.out.println("Thank you for ordering with Grapefruit Company, " + CustomerName);
        else if  (nNumber != SENTINEL) 
        System.out.println("Please select another item from the menu above: ");
        } //End Loop



        //Process selections entered by the user

            //Increment count 


            //Print blank line to screen
            System.out.println("");

            //Total amount of product ordered
            System.out.println("Total items ordered: ");

            //Total price of items ordered
            System.out.println("Price of items ordered: ");

            //Sales tax associated with the purchase
            System.out.println("Sales tax: " + SALES_TAX);

            //Total amount due by the customer to Grapefruit Co. 
            System.out.println("Total amount due: ");


      }

    }

2 个答案:

答案 0 :(得分:2)

对于某些要添加的类来说,这是非常令人烦恼的,因此您可以轻松地遍历这些项目。

然而,你可以在这个场景中做些什么:

  • 保持数组中项目的价格。

    int [] itemPrices = {49,299,329,399,199,1299,1199,999,599};
    
  • 更新您的打印报表

    System.out.println("1. gPod shuffle $" + prices[0]);
    
    System.out.println("2. gPod Touch   $" + prices[1]);
    
    System.out.println("3. gPad Mini    $" + prices[2]);
    
    System.out.println("4. gPad 2       $" + prices[3]);
    
    System.out.println("5. gPhone       $" + prices[4]);
    
    System.out.println("6. gMac         $" + prices[5]);
    
    System.out.println("7. MacNovel Pro $" + prices[6]);
    
    System.out.println("8. MacNovel Air $" + prices[7]);
    
    System.out.println("9. MiniMac      $" + prices[8]);
    
  • 更新while循环以引用这些商品价格

    // Keep reading until the input is 10
    while (nNumber != SENTINEL) 
    {
    
        System.out.println("\nPlease select an item from the menu above: ");
    
        // Read number entered by the user
        nNumber = input.nextInt();
    
        if (nNumber == SENTINEL) 
        {   
            System.out.println("Thank you for ordering with Grapefruit Company, " + CustomerName);
            // The user's just entered the value held for SENTINEL - leave the while loop.
            break;
        } 
    
        // Calculate the total price.
        nTotal = nTotal + prices[nNumber-1];
    
    
        // Increment the total number of products entered.
        nProducts++;
    }
    
  • 更新最终的打印声明:

        //Total amount of product ordered
        System.out.println("Total items ordered: " + nProducts);
    
        //Total price of items ordered
        System.out.println("Price of items ordered: "+nTotal);
    
        //Sales tax associated with the purchase
        System.out.println("Sales tax: " + SALES_TAX);
    

我会告诉你如何得到应付的总金额:)

答案 1 :(得分:1)

要解决您遇到的此连接问题:

System.out.println("Total amount due: " + ((SALES_TAX * nTotal) + nTotal));

这意味着在将其转换为字符串之前执行完整计算。

但是这背后的数学是错误的,而是执行以下操作:

// Calculate the amount of tax.
double salesTax = (SALES_TAX/100) * nTotal;
System.out.println("Sales tax: " + salesTax);

然后你应该这样做:

//Total amount due by the customer to Grapefruit Co. 
System.out.println("Total amount due: " + (salesTax + nTotal));