用 Java 代码创建一个简单的销售计算器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9928459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a simple sales calculator in Java code
提问by J Reed
I'm trying to figure out how to write this?
我想弄清楚如何写这个?
Write a Java program that will serve as a basic sales calculator. The program should present the user with a choice of four different products of your choosing in a simple menu. After the user selects a product by entering a character corresponding to a product, the program should prompt the user for the quantity and then calculate the Subtotal, Sales Tax Amount, and Total Sale Amount. The calculations should be performed as follows:
编写一个 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
Be sure to use variables to store temporary values. The program should output the product sold, quantity sold, and the calculated values for Subtotal, Sales Tax Amount, and Total Sale Amount. Your assignment submittal should include your java code which is properly commented, and the class file..
请务必使用变量来存储临时值。该程序应输出已售产品、已售数量以及小计、销售税金额和总销售金额的计算值。您的作业提交应包括正确注释的 Java 代码和类文件..
this is what i have so far and not sure if I am on the right track?
这是我到目前为止所拥有的,不确定我是否在正确的轨道上?
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;
}
}
回答by Mo Kouli
Since this is a homework question, I won't be providing you with the answer to your problem, but hopefully I will be able to help you in figuring out how to structure your program, as well as explain how to use the Scanner class to gather input from the user. The rest will be up to you.
由于这是一个家庭作业问题,我不会为您提供问题的答案,但希望我能够帮助您弄清楚如何构建您的程序,并解释如何使用 Scanner 类来收集用户的输入。剩下的就靠你了。
First you will need to develop the pseudo-code for your main program. Basically a flow of execution based on the events that should happen.
首先,您需要为主程序开发伪代码。基本上是基于应该发生的事件的执行流程。
pseudo-code is NOT code that will compile, but is useful in figuring out the structure of a program. Here is the pseudo code for your program.
伪代码不是可以编译的代码,但可用于确定程序的结构。这是您程序的伪代码。
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
Notice that upon successful completion of getting the total cost of a purchase, we "restart the program". If you were more advanced, this might entail calling a function, but my guess is that you are still a beginner, so doing the same thing more than once should remind you of a loop. In this case a while loop.
请注意,在成功完成获取购买总成本后,我们“重新启动程序”。如果你更高级,这可能需要调用一个函数,但我的猜测是你仍然是一个初学者,所以不止一次做同样的事情应该会让你想起一个循环。在这种情况下是一个 while 循环。
Thus we can rewrite this pseudocode to the following
因此,我们可以将此伪代码重写为以下内容
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
Now, notice how when the user inputs an invalid quantity (ie: Something that is not an integer > 1) we ask for a quantity AGAIN. Doing the same thing multiple times? That's right, that means we should probably use another while loop again.
现在,请注意当用户输入无效数量(即:不是整数 > 1 的东西)时,我们如何再次要求数量。多次做同样的事情?没错,这意味着我们可能应该再次使用另一个 while 循环。
For this second while loop, the basic thinking is, "keep asking the user for a quantity until we have a valid quantity". The simplest way to accomplish this, is to create a boolean variable we call haveQuantity, and loop until that value is true.
对于第二个 while 循环,基本思想是“不断向用户询问数量,直到我们获得有效数量”。实现这一点的最简单方法是创建一个布尔变量,我们称之为 haveQuantity,并循环直到该值为真。
Our pseudo-code now becomes:
我们的伪代码现在变成:
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
This should be the general structure of your program. In the following section, I will show you how to properly use the scanner class to get input from the user.
这应该是您程序的一般结构。在下一节中,我将向您展示如何正确使用扫描仪类来获取用户的输入。
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.
}
}
Hope this helps. And feel free to ask more questions.
希望这可以帮助。并随时提出更多问题。
回答by dm03514
Stackoverflow really excels when you have a very specific question to ask. As for your requirements, you are asking the user for input thats good. But you are not mapping items to prices or quantities. You are hardcoding in the items position ie "3) I-Pad 3" which will make it harder to get the actual item name later and match it to it's price.
当你有一个非常具体的问题要问时,Stackoverflow 真的很出色。至于您的要求,您要求用户提供好的输入。但是您没有将项目映射到价格或数量。您正在对商品位置进行硬编码,即“3) I-Pad 3”,这将使以后获取实际商品名称并将其与其价格相匹配变得更加困难。