【问题标题】:How to add sum multiple statements in a while loop in Java如何在Java中的while循环中添加多个语句
【发布时间】:2016-02-12 22:50:22
【问题描述】:

我是 java 的初学者。我有一个简单的 java 编程作业。这是程序的基础:

“Best Purchase 接受电子设备和配件的在线订单。他们开发了一个程序,允许用户从项目菜单中选择要购买的设备。编写一个程序,允许用户(客户)选择要购买的商品,计算所订购商品的价格,并向客户显示此信息的收据(假设销售税率为 6.5%)”

您的程序应提示用户(客户)输入以下信息:

  • 客户名称
  • 购买的电子设备和配件

然后程序输出以下信息:

  • 客户姓名
  • 订购的商品总数
  • 销售税前菜单项的价格
  • 营业税
  • 应付总额

这是我的问题,如果取决于用户选择的数字,如何在嵌套中添加值?

例如:用户选择 1,2,3,10(退出循环)

这是我的代码:

import java.util.Scanner;
public class OnlinePurchase {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        //Declare Variables
        Scanner input = new Scanner(System.in);
        String sCustomerName ="";   //Stores the Customer's Name
        int nSelection = 0;         //Stores the value entered by the user
        int nSum = 0;               //Stores sum of values entered
        int nCount = 0;             //Number of values entered
        int nPrice = 0;

        //Declare Constants
        final int SENTINEL = 10; //Used to end loop

        //Promt User to enter the their name.
        System.out.print("Please enter the your name: ");
        //Read Customer Name input from user
        sCustomerName = input.nextLine( );    

        //Print to Blank Line for spacing
        System.out.println("\n");

        //Print Purchase Menu
        System.out.println("BEST PURCHASE PRODUCTS:");
        System.out.println("1. Smartphone..........$249");
        System.out.println("2. Smartphone Case.... $39");
        System.out.println("3. PC Laptop...........$1149");
        System.out.println("4. Tablet..............$349");
        System.out.println("5. Tablet Case.........$49");
        System.out.println("6. eReader.............$119");
        System.out.println("7. PC Desktop..........$889");
        System.out.println("8. LED Monitor.........$299");
        System.out.println("9. Laser Printer.......$399");
        System.out.println("10.Complete my order");

        //Print to Blank Line for spacing
        System.out.println("\n");

        while (nSelection != SENTINEL) {

            //Calculate sum
            nSum = nPrice + nSum;

            //Increment counter
            nCount++; //or nCount = nCount + 1;

            //Promt User to enter the an item from the menu.
            System.out.print("Please enter item from the menu above: ");
            //Read input from user for selected purchase
            nSelection = input.nextInt();

            if (nSelection == 1) {

                nPrice = 249;

                //Promt User to enter the an item from the menu.
                System.out.print("Please enter another item from the menu above: ");
                //Read input from user for selected purchase
                nSelection = input.nextInt();
            }
            else if (nSelection == 2 ) {

                nPrice = 39;

                //Promt User to enter the an item from the menu.
                System.out.print("Please enter another item from the menu above: ");
                //Read input from user for selected purchase
                nSelection = input.nextInt();
            }
            else if (nSelection == 3 ) {

                nPrice = 1149; 

                //Promt User to enter the an item from the menu.
                System.out.print("Please enter another item from the menu above: ");
                //Read input from user for selected purchase
                nSelection = input.nextInt();
            }
            else if (nSelection == 4 ) {

                  nPrice = 349;

                  //Promt User to enter the an item from the menu.
                  System.out.print("Please enter another item from the menu above: ");
                  //Read input from user for selected purchase
                  nSelection = input.nextInt();
            }
            else if (nSelection == 5 ) {

                nPrice = 49;

                //Promt User to enter the an item from the menu.
                System.out.print("Please enter another item from the menu above: ");
                //Read input from user for selected purchase
                nSelection = input.nextInt();
            }
            else if (nSelection == 6 ) {

                nPrice = 119;

                //Promt User to enter the an item from the menu.
                System.out.print("Please enter another item from the menu above: ");
                //Read input from user for selected purchase
                nSelection = input.nextInt();
            }
            else if (nSelection == 7 ) {

                nPrice = 899;

                //Promt User to enter the an item from the menu.
                System.out.print("Please enter another item from the menu above: ");
                //Read input from user for selected purchase
                nSelection = input.nextInt();
            }
            else if (nSelection == 8 ) {

                nPrice = 299;

                //Promt User to enter the an item from the menu.
                System.out.print("Please enter another item from the menu above: ");
                //Read input from user for selected purchase
                nSelection = input.nextInt();
            }  
            else if (nSelection == 9 ) {

                nPrice = 399;

                //Promt User to enter the an item from the menu.
                System.out.print("Please enter another item from the menu above: ");
                //Read input from user for selected purchase
                nSelection = input.nextInt();
            }
        }//end while user did not enter 10

        //Print blank line
        System.out.println();

        //Print Thank You message
        System.out.println("Thank you for ordering with Best Purchase,"+sCustomerName);
        //Print number of integers entered
        System.out.println("Total Items Ordered: " + nCount);
         //Print number of integers entered
        System.out.println("Total: $" + nSum);  
    }//end main class
}//end public class

【问题讨论】:

  • Stack Overflow 不是家庭作业完成服务。
  • 首先,如果你想让用户选择多个项目,你必须循环运行你的逻辑。其次,您可以使用nPrice += 119; 而不是nPrice = 119; - 例如...
  • @redFIVE 您是否完全阅读了他的问题?他没有要求我们做他的硬件。你应该撤回你的反对票。
  • 您的代码不是已经按照您的要求执行了吗?您已经有一个循环,并且您已经在添加项目的值...
  • @alfasin,需求转储,代码转储 + Here is my issue how to i add the values inside my nested if depending on what numbers the user select?。这怎么不是作业完成请求?另外,我会随心所欲地投反对票。

标签: java while-loop


【解决方案1】:

老实说,您的代码过于复杂,而且有很多重复。 您应该利用重用方法以及可能的 switch 语句来处理选择正确的价格。此外,您不应将所有程序逻辑都放在 main 方法中——利用更小、更简洁的方法,并尽量减少放在 main() 中的内容。

您的 while 循环应该简单地接受下一行输入,调用 processItem() 方法并在输入的数字是您的 Sentinel 值 (10) 时终止。

试试这个版本的代码:

import java.util.Scanner;

public class OnlinePurchase {

    String customerName;
    int nSum = 0;               //Stores sum of values entered
    int nCount = 0;             //Number of values entered

    public void printPurchaseMenu(){
        //Print Purchase Menu
        System.out.println("\nBEST PURCHASE PRODUCTS:");
        System.out.println("1. Smartphone..........$249");
        System.out.println("2. Smartphone Case.... $39");
        System.out.println("3. PC Laptop...........$1149");
        System.out.println("4. Tablet..............$349");
        System.out.println("5. Tablet Case.........$49");
        System.out.println("6. eReader.............$119");
        System.out.println("7. PC Desktop..........$889");
        System.out.println("8. LED Monitor.........$299");
        System.out.println("9. Laser Printer.......$399");
        System.out.println("10.Complete my order");
    }

    public void setCustomerName(String name){
        customerName = name;
    }

    public void processInput(int inputValue){

        switch(inputValue){
          case 1: addItem(249); break;
          case 2: addItem(39); break;
          case 3: addItem(1149); break;
          case 4: addItem(349); break;
          case 5: addItem(49); break;
          case 6: addItem(119); break;
          case 7: addItem(889); break;
          case 8: addItem(299); break;
          case 9: addItem(399); break;
          case 10: printResults(); break;
          default: System.out.println("You entered an invalid value.");
        }
    }

    public void addItem(int price){
        nSum += price;
        nCount++;
    }

    public void printResults(){
        //Print Thank You message
        System.out.println("\nThank you for ordering with Best Purchase," + customerName);
        //Print number of integers entered
        System.out.println("Total Items Ordered: " + nCount);
         //Print number of integers entered
        System.out.println("Total: $" + nSum);
    }


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        OnlinePurchase onlinePurchase = new OnlinePurchase();

        //Declare Variables
        Scanner input = new Scanner(System.in);
        int nSelection = 0;  //Stores the value entered by the user

        //Declare Constants
        final int SENTINEL = 10; //Used to end loop

        //Prompt User to enter the their name.
        System.out.print("Please enter the your name: ");

        //Read Customer Name input from user
        onlinePurchase.setCustomerName(input.nextLine( ));

        //Print Purchase Menu
        onlinePurchase.printPurchaseMenu();

        while (nSelection != SENTINEL){
            System.out.print("Please enter item from the menu above: ");
            nSelection = input.nextInt();

            onlinePurchase.processInput(nSelection);
            System.out.println("\n");
      }//end while user did not enter 10
  }//end main method
}//end public class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 2018-12-27
    • 2018-05-27
    • 2014-08-20
    相关资源
    最近更新 更多