Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Java代码创建一个简单的销售计算器_Java - Fatal编程技术网

用Java代码创建一个简单的销售计算器

用Java代码创建一个简单的销售计算器,java,Java,我在想怎么写这个 编写一个Java程序作为基本的销售计算器。该程序应在一个简单的菜单中为用户提供您选择的四种不同产品。用户通过输入与产品对应的字符选择产品后,程序应提示用户输入数量,然后计算小计、增值税金额和总销售额。应按照以下方式进行计算: Subtotal = (Price * Quantity) Sales Tax Amount = Subtotal * Sales Tax Percentage (use 6.5% for the sales tax percentage) Total

我在想怎么写这个

编写一个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;


}

}

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

由于这是一个家庭作业问题,我不会为您提供问题的答案,但希望我能够帮助您了解如何构建程序,以及解释如何使用Scanner类从用户收集输入。剩下的就看你了

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

伪代码不是要编译的代码,但在计算程序结构时很有用。这是您的程序的伪代码

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
这应该是程序的总体结构。在下一节中,我将向您展示如何正确使用scanner类从用户获取输入

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.
    }
}

希望这有帮助。您还可以自由提问。

我需要让用户有输入。。因此,屏幕需要反映这一点。有没有其他方法来解决这个问题?@JReed:我建议使用
类产品
,它至少有
名称
价格
属性。你可以创建一个数组,列出它们的价格,你想要多少就有多少(你只需要把用户的输入转换成一个整数,减去1就可以得到你的项目的索引,而不是编写代码来检查每个项目)…等等。让我拿出我的书,学习如何编写java。谢谢你能建议使用and数组吗?这样名称=价格,然后如果用户输入是x,那么总数就等于?@JReed:数组将使显示项目和查找项目变得非常简单。如果用户输入x,则项目将是
items[x-1]
,并包含价格。我不确定我是否回答了你的问题,因为坦率地说,这是胡言乱语。谢谢,我刚刚开始理解如何编写java代码。我感谢所有能得到的帮助。我只是对很多事情感到困惑,一直在翻阅一本书,试图找出答案。