我正在试图弄清楚如何写这个?
编写一个将作为基本销售计算器的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;
}
}
答案 0 :(得分:1)
由于这是一个家庭作业问题,我不会为您提供问题的答案,但希望我能帮助您弄清楚如何构建您的程序,以及解释如何使用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的东西),我们要求数量为AGAIN。多次做同样的事情?这是对的,这意味着我们应该再次使用另一个while循环。
对于第二个while循环,基本思路是“在我们拥有有效数量之前不断询问用户数量”。完成此操作的最简单方法是创建一个我们称为hasQuantity的布尔变量,并循环直到该值为true。
我们的伪代码现在变为:
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.
}
}
希望这会有所帮助。并随意提出更多问题。
答案 1 :(得分:0)