Java柠檬汁计算器
任务是:Java柠檬汁计算器,java,string,variables,constants,calculator,Java,String,Variables,Constants,Calculator,任务是: 在输出屏幕顶部显示任何欢迎信息 创建变量来保存一杯柠檬水的价格值 显示每杯的价格 询问用户的姓名,并将其存储为字符串对象。只要有可能,请按名称引用用户 询问用户想要订购多少杯柠檬水。将其保存为具有适当数据类型的变量 将圣地亚哥8%的税率作为恒定变量存储在您的计划中 计算小计、总税金和总价,并将其显示在屏幕上 询问用户想要如何支付柠檬水,并将输入保存为char变量 要求用户输入“m”表示货币,“c”表示信用卡,或“g”表示黄金 使用DecimalFormat类,使打印到屏幕上的所有货币
- 在输出屏幕顶部显示任何欢迎信息
- 创建变量来保存一杯柠檬水的价格值
- 显示每杯的价格
- 询问用户的姓名,并将其存储为字符串对象。只要有可能,请按名称引用用户
- 询问用户想要订购多少杯柠檬水。将其保存为具有适当数据类型的变量
- 将圣地亚哥8%的税率作为恒定变量存储在您的计划中
- 计算小计、总税金和总价,并将其显示在屏幕上
- 询问用户想要如何支付柠檬水,并将输入保存为char变量
- 要求用户输入“m”表示货币,“c”表示信用卡,或“g”表示黄金
- 使用DecimalFormat类,使打印到屏幕上的所有货币数据显示2位小数,并显示一个“$”符号
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));
}
看起来你已经将增值税设置为常量,这就是“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));
}
public static final double CA\u SALES\u TAX=0.08;
-但是您需要在类级别(不在您的方法中)声明它public static final double CA\u SALES\u TAX=0.08;
-但是您需要在类级别(不在您的方法中)声明它