java - 编程存储数量并编写 else if 语句以显示正确的
问题描述
最后的程序将显示最终的估算报告。如果用户只要求一张表,则输出将显示最终总估计值,该表为单数。如果用户要求多个表,输出将显示表的数量和总成本。如果用户根本没有请求任何估计,程序应该显示一条消息说明这一点。
它只显示“您没有估计任何表格......”
public class Table {
public static void main(String[] args) {
String material;
int tChoice;
int mChoice;
int numTable = 0;
double tableCost;
double total = 0;
double pLaminate = 0.125;
double pOak = 0.25;
Scanner input = new Scanner(System.in);
System.out.println("Welcome to Tables are Us - Your One Stop Table Shop");
System.out.println("Mike - Master Table Builder");
boolean bFlag = true;
while (bFlag) {
double area = 0;
double length = -1;
double width = -1;
double diameter = -1;
double radius = -1;
do {
System.out.println("\n\nWhat shape of table do you wish to build?");
System.out.println("\t1. Rectangular");
System.out.println("\t2. Sqaure");
System.out.println("\t3. Circular");
System.out.println("\t4. End Program");
System.out.print("Enter menu entry: ");
tChoice = input.nextInt();
if (tChoice < 1 || tChoice > 4) {
System.out.println("Error - Invalid Entry. Please reenter a valid value.");
}
} while (tChoice < 1 || tChoice > 4);
if (tChoice == 1) {
while (length < 0) {
System.out.print("Enter the length of the table (in inches): ");
length = input.nextDouble();
if (length < 0) {
System.out.println("Error - the length must be greater than zero. Please reenter!!");
}
}
while (width < 0) {
System.out.print("Enter the width of the table (in inches): ");
width = input.nextDouble();
if (width < 0) {
System.out.println("Error - the width must be greater than zero. Please reenter!!");
}
}
area = length * width;
} else if (tChoice == 2) {
while (length < 0) {
System.out.print("Enter the length of the table (in inches): ");
length = input.nextDouble();
if (length < 0) {
System.out.println("Error - the length must be greater than zero. Please reenter!!");
}
}
area = length * length;
} else if (tChoice == 3) {
while (diameter < 0) {
System.out.print("Enter the diameter of the table (in inches): ");
diameter = input.nextDouble();
if (diameter < 0) {
System.out.println("Error - the diameter must be greater than zero. Please reenter!!");
}
}
radius = diameter / 2;
area = Math.PI * radius * radius;
} else if (tChoice == 4) {
if (numTable == 0) {
System.out.println("\nYou did not estimate any tables today!");
} else {
System.out.printf("The total cost of the " + numTable + "tables you estimated is $" + String.format("%.2f", total));
}
System.out.println("Thank you for using the table cost estimation program!");
System.out.println("GoodBye!!!");
input.close();
return;
}
do {
System.out.println("\nWhat type of material do you want to use?");
System.out.println("\t1. Laminate ($0.125 per square inch)");
System.out.println("\t2. Oak ($0.25 per square inch)");
System.out.print("Enter menu entry: ");
mChoice = input.nextInt();
if (mChoice < 1 || mChoice > 2) {
System.out.println("Error - Invalid Entry. Please reenter a valid value.");
}
} while (mChoice < 1 || mChoice > 2);
if (mChoice == 1) {
material = "Laminate";
tableCost = area * pLaminate;
} else {
material = "Oak";
tableCost = area * pOak;
}
System.out.println("\nOutput Report:");
System.out.printf("The area of table is " + String.format("%.2f", area) + " square inches");
System.out.printf("\nThe table will be made of " + material);
System.out.printf("\nThe cost of the table is $" + String.format("%.2f", tableCost));
total = total + tableCost;
total++;
}
input.close();
}
}
解决方案
System.out.printf("The total cost of the %d tables you estimated is $%.2f%n", numTable, total);
打印格式的末尾需要换行符%n
。确保numTable
已设置。
推荐阅读
- c++ - 将数据从 c++ 文件连续发送到 Python 脚本以进行进一步处理
- c++ - 如何使用多线程生成事件以及如何在 C++ 中同步它们?
- bootstrap-4 - 无法让引导网格与 shopify 购买按钮一起使用
- java - 如何将 Alfresco 查询结果集限制为仅前 N 行
- asp.net-core - 调用发布到服务器的页面后,ASP.Net Core Web Api 不起作用
- android - 我的 android 应用程序在版本 8 (Oreo) 中工作,但在版本 4.4 (KitKat) 中不工作
- asp.net-identity - 可以使用 AccessToken 或 Identity 令牌作为 Connect-AzureAD / MSGraph 的凭据吗?
- python - pyspark中的case语句问题
- javascript - 从导入的组件中读取值
- sql - PL SQL 过程不引发异常 no_data_found