分配给:
在输出屏幕顶部显示任何欢迎消息
创建变量来保存一杯柠檬水的价格值。
显示每杯的价格。
询问用户的姓名,并将其存储为 String 对象。尽可能称呼用户的姓名。
询问用户想要订购多少杯柠檬水。将其保存为具有适当数据类型的变量。
将圣地亚哥 8% 的税率存储为程序中的常量变量。
计算小计、税金总额和总价,并将其显示在屏幕上。
询问用户如何支付柠檬水费用,并将输入保存为 char 变量。
要求用户输入“m”(代表金钱)、“c”(信用卡)或“g”(代表金币)
使用 DecimalFormat 类,使打印到屏幕上的所有货币数据显示 2 位小数,并显示“$”符号。
需要帮助弄清楚如何在我的程序中将 8% 的税率作为常量变量 这样我就可以计算小计、总税和总价,并将其显示在屏幕上
到目前为止,这就是我所拥有的:
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class FirstProgram {
public static void main(String[] args) {
double cost = 7.55;
double amount = 7.55;
final double CA_SALES_TAX = 0.08;
int tax, subtotal, total;
subtotal = (int) (amount * cost);
tax = (int) (subtotal * CA_SALES_TAX);
total = tax + subtotal;
Scanner input = new Scanner(System.in);
double fnum = 7.55, tax1 = fnum * 0.08, answer = tax1 + fnum;
System.out.println("Welcome to the best Lemonade you'll ever taste! ");
System.out.println("My lemonade would only cost you a measly: $" + amount);
System.out.println("What is your name?");
String first_name;
first_name = input.nextLine();
System.out.println("Hi " +first_name+ ", how many glasses of lemonade would you like?");
fnum = input.nextDouble();
System.out.println("Subtotal: $" + (amount * fnum));
System.out.println("Tax: $" + (tax1 * CA_SALES_TAX));
tax1 = input.nextDouble();
感谢任何帮助
最佳答案
您似乎已经将销售税设置为常量,这就是“final”关键字的用途。至于你的代码,我看到一些冗余,并且不确定你为什么要转换为整数。我根据我认为你想要它做的事情做了一些修改。
public static void main(String[] args) {
double cost = 7.55;
final double CA_SALES_TAX = 0.08;
double subtotal,tax,total;
Scanner input = new Scanner(System.in);
System.out.println("Welcome to the best Lemonade you'll ever taste! ");
System.out.println("My lemonade would only cost you a measly: $" + cost);
System.out.println("What is your name?");
String first_name = input.nextLine();
System.out.println("Hi " +first_name+ ", how many glasses of lemonade would you like?");
int fnum = input.nextInt();
//calc subtotal, tax, total
subtotal = fnum * cost;
tax = subtotal *CA_SALES_TAX;
total = tax + subtotal;
// print them all out
System.out.println("Subtotal: $" + (subtotal));
System.out.println("Tax: $" + (tax));
System.out.println("Total Price: $" + (total));
}
关于Java 柠檬水计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32751324/