【问题标题】:Creating a simple sales calculator in Java code用 Java 代码创建一个简单的销售计算器
【发布时间】:2013-04-15 00:29:44
【问题描述】:

我在想怎么写?

编写一个用作基本销售计算器的 Java 程序。该程序应该在一个简单的菜单中向用户展示您选择的四种不同产品。用户通过输入与产品对应的字符选择产品后,程序应提示用户输入数量,然后计算小计、销售税金额和总销售额。应按如下方式进行计算:

Subtotal = (Price * Quantity)

Sales Tax Amount = Subtotal * Sales Tax Percentage (use 6.5% for the sales tax percentage)

Total Sale Amount = Subtotal + Sales Tax Amount

请务必使用变量来存储临时值。程序应输出销售的产品、销售的数量以及小计、销售税金额和总销售金额的计算值。您的作业提交应包括正确注释的 Java 代码和类文件..

这是我目前所拥有的,不确定我是否走在正确的轨道上?

import java.util.scanner; 

public class Sales //Defines the class
    Public static void main (String args[]){

    System.out.println("Welcome to Super Shopper");
    System.out.println("1) Star Wars DVD");
    System.out.println("2) X-box 360 ");
    System.out.println("3) I-Pad 3");
    System.out.println(“4) 2 liter Soda”);
    System.out.println("5) Quit");

    Scanner sc  = new Scanner (System.in);

    System.out.print("Please select item :");

    int choice = keyword.nextInt();

    Scanner number = new scanner (System.in);

    System.out.print("Please select quantity :");

    Int choice = quantity.nextInt();

    String userInput;


}

}

【问题讨论】:

    标签: java


    【解决方案1】:

    由于这是一个家庭作业问题,因此我不会为您提供问题的答案,但希望我能够帮助您弄清楚如何构建您的程序,并解释如何使用用于收集用户输入的扫描程序类。剩下的就看你自己了。

    首先,您需要为您的主程序开发伪代码。基本上是基于应该发生的事件的执行流程。

    伪代码不是可以编译的代码,但在弄清楚程序的结构时很有用。这是您的程序的伪代码。

    show greeting with choices.
    
    get choice from user
    
    if choice is valid and choice is not exit
        prompt user for quantity
            if quantity is valid 
                calculate total and show it to the user
                restart program
    
            if quantity is invalid
                prompt user for a valid quantity again
    
     if choice is valid and choice is exit
         show exit message and exit program
    
     if choice is invalid
         show error message and restart program
    

    请注意,在成功获取购买总成本后,我们会“重新启动程序”。如果你更高级,这可能需要调用一个函数,但我猜你还是个初学者,所以不止一次做同样的事情应该会让你想起一个循环。在这种情况下是一个while循环。

    因此我们可以把这个伪代码改写成下面的样子

    done = false
    while not done
        get choice from user
    
        if choice is valid and choice is not exit
            prompt user for quantity
                if quantity is valid 
                    calculate total and show it to the user
    
                if quantity is invalid
                    prompt user for a valid quantity again
    
        if choice is valid and choice is exit
            done = true
    
        if choice is not valid
            show error message
    
    exit program
    

    现在,请注意当用户输入无效数量(即:不是整数 > 1 的内容)时,我们如何再次请求数量。多次做同样的事情?没错,这意味着我们可能应该再次使用另一个 while 循环。

    对于第二个 while 循环,基本思路是“不断向用户询问数量,直到我们获得有效数量”。实现这一点的最简单方法是创建一个我们称为 haveQuantity 的布尔变量,然后循环直到该值为真。

    我们的伪代码现在变成:

    done = false
    while not done
        get choice from user
    
        if choice is valid and choice is not exit
            haveQuantity = false
            while not haveQuantity
                prompt user for quantity
                    get quantity from user
                    if quantity is valid 
                        haveQuantity = true
                        calculate total and show it to the user
    
        if choice is valid and choice is exit
            done = true
    
        if choice is not valid
            show error message
    
    exit program
    

    这应该是您程序的一般结构。在下一节中,我将向您展示如何正确使用扫描仪类来获取用户的输入。

    public class EchoInt
    {
        import java.util.Scanner;
    
        public static void main(String[] args)
        {
                //Declaration of variables outside the while loop
                Scanner scan = new Scanner(System.in); //declaring variables outside of a loop saves space and speeds up execution as the jvm does not need to reallocate space for an object inside the loop.
    
        boolean done = false; //this will be our conditional for the while loop
        int input = -1; 
    
        while(!done) //while done is equal to false.
        {
                System.out.println("Please enter a positive int to echo or 0 to exit: ");
    
            if(scan.hasNextInt()) //If the user has inputted a valid int
                input = scan.nextInt(); //set the value of input to that int.
    
            else //The scanner does not have a integer token to consume
            {    
                         /*
                         THIS IS IMPORTANT. If the scanner actually does have a token
                         which was not an int. For example if the user entered a string,
                         you need to consume the token to prepare to accept further tokens.
                         */
                         if(scan.hasNext()) 
                             scan.next(); //Actually consumes the token
    
                         input = -1; //This is used to indicate that an invalid input was submitted
                     }
    
             if(input == 0) //The user chose to exit the program
                 done = true; //set done to true to kick out of the while loop
    
             else if(input == -1) //This means the user inputed an invalid input
             System.out.println("ERROR! Try again."); //show error message
    
             else //The user inputted valid input
                 System.out.println("echo: "+input); //Echo the int
    
        }
    
                scan.close(); //We are done, so close the scanner
            System.out.println("Exiting. Goodbye!"); //Show a goodbye message
            System.exit(0); //exit the program. The zero tells us we exited without errors.
        }
    }
    

    希望这会有所帮助。并随时提出更多问题。

    【讨论】:

    • 谢谢,我正处于了解如何编写 java 代码的最开始阶段。我很感激我能得到的所有帮助。我只是对很多事情感到困惑,并且一直在翻阅书籍试图弄清楚。
    【解决方案2】:

    当您有一个非常具体的问题要问时,Stackoverflow 真的很出色。至于您的要求,您要求用户输入很好。但是您没有将项目映射到价格或数量。您在商品位置进行硬编码,即“3) I-Pad 3”,这将使以后更难获得实际商品名称并将其与价格匹配。

    【讨论】:

    • 我需要让用户输入..所以屏幕需要反映这一点。有没有其他方法可以写出这个问题?
    • @JReed:我建议class Product 至少具有name 和price 属性。你可以创建一个数组,列出它们的价格,有多少你想要的(你只需要将用户的输入转换为一个 int 并减去 1 来获取你的项目的索引,而不是编写代码来检查每一个)...等等等等等等。
    • 让我把我的书拿出来,只是学习如何编写java。谢谢
    • 您是否建议使用 and 数组,这样 name = price 然后 a 如果用户输入是 x,那么 total 将等于?
    • @JReed:数组可以让显示项目和查找项目变得更加容易。如果用户输入 x,则该项目将为 items[x-1],并且其中包含价格。我不确定我是否回答了你的问题,坦率地说,这是胡言乱语。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多