英文:
The programming storing quantities and writing else if statement to display the correct one
问题
以下是你提供的代码的翻译部分:
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("欢迎来到“桌子之家” - 您的一站式桌子购物中心");
System.out.println("迈克 - 主要桌子建造师");
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\n您想要构建什么形状的桌子?");
System.out.println("\t1. 长方形");
System.out.println("\t2. 正方形");
System.out.println("\t3. 圆形");
System.out.println("\t4. 结束程序");
System.out.print("输入菜单项:");
tChoice = input.nextInt();
if (tChoice < 1 || tChoice > 4) {
System.out.println("错误 - 无效的输入。请重新输入有效的值。");
}
} while (tChoice < 1 || tChoice > 4);
if (tChoice == 1) {
while (length < 0) {
System.out.print("输入桌子的长度(以英寸为单位):");
length = input.nextDouble();
if (length < 0) {
System.out.println("错误 - 长度必须大于零。请重新输入!");
}
}
while (width < 0) {
System.out.print("输入桌子的宽度(以英寸为单位):");
width = input.nextDouble();
if (width < 0) {
System.out.println("错误 - 宽度必须大于零。请重新输入!");
}
}
area = length * width;
} else if (tChoice == 2) {
while (length < 0) {
System.out.print("输入桌子的长度(以英寸为单位):");
length = input.nextDouble();
if (length < 0) {
System.out.println("错误 - 长度必须大于零。请重新输入!");
}
}
area = length * length;
} else if (tChoice == 3) {
while (diameter < 0) {
System.out.print("输入桌子的直径(以英寸为单位):");
diameter = input.nextDouble();
if (diameter < 0) {
System.out.println("错误 - 直径必须大于零。请重新输入!");
}
}
radius = diameter / 2;
area = Math.PI * radius * radius;
} else if (tChoice == 4) {
if (numTable == 0) {
System.out.println("\n您今天没有估算任何桌子!");
} else {
System.out.printf("您估算的 %d 张桌子的总成本为 $%.2f", numTable, total);
}
System.out.println("感谢您使用桌子成本估算程序!");
System.out.println("再见!");
input.close();
return;
}
do {
System.out.println("\n您想要使用什么类型的材料?");
System.out.println("\t1. 层压板(每平方英寸 $0.125)");
System.out.println("\t2. 橡木(每平方英寸 $0.25)");
System.out.print("输入菜单项:");
mChoice = input.nextInt();
if (mChoice < 1 || mChoice > 2) {
System.out.println("错误 - 无效的输入。请重新输入有效的值。");
}
} while (mChoice < 1 || mChoice > 2);
if (mChoice == 1) {
material = "层压板";
tableCost = area * pLaminate;
} else {
material = "橡木";
tableCost = area * pOak;
}
System.out.println("\n输出报告:");
System.out.printf("桌子的面积为 %.2f 平方英寸", area);
System.out.printf("\n桌子将使用 %s 制作", material);
System.out.printf("\n桌子的成本为 $%.2f", tableCost);
total = total + tableCost;
total++;
}
input.close();
}
}
英文:
The program at the end will display the final estimation report. If the user only asked for one table, the output will display the final total estimate with the table as a singular. If the user asked for multiple tables, the output will display the number of tables and the total cost. If the user did not request any estimates at all, the program should display a message stating that.
It only displays "You did not estimate any tables...."
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();
}
}
答案1
得分: 1
System.out.printf("你估计的 %d 张桌子的总费用为 $%.2f%n", numTable, total);
Print-formatted 需要在末尾加上换行符 %n
。
确保 numTable
已设置。
英文:
System.out.printf("The total cost of the %d tables you estimated is $%.2f%n", numTable, total);
Print-formatted needs a newline at the end %n
.
Ensure that numTable
is set.
答案2
得分: 0
public class Table {
public static void main(String[] args) {
System.out.println("欢迎来到“我们的桌子之家” - 您的一站式桌子购物中心");
System.out.println("迈克 - 主要桌子建造师");
int totalTables = 0;
double totalTableCost = 0;
Scanner scan = new Scanner(System.in);
scan.useLocale(Locale.ENGLISH);
while (true) {
int shapeMenuItem = getShapeMenuItem(scan);
if (shapeMenuItem == 4) {
printFinalResults(totalTables, totalTableCost);
return;
}
double area = getArea(scan, shapeMenuItem);
if (Double.isNaN(area))
System.err.println("错误 - 无效的输入。请重新输入有效值。");
else {
int materialMenuItem = getMaterialMenuItem(scan);
double tableCost = area * getMaterialCost(materialMenuItem);
totalTableCost += tableCost;
totalTables++;
printReport(area, tableCost, materialMenuItem);
}
}
}
private static void printFinalResults(int numTable, double total) {
if (numTable == 0)
System.out.println("您今天没有估计任何桌子!");
else
System.out.format(Locale.ENGLISH, "\n您估计的%d张桌子的总费用为$%.2f\n", numTable, total);
System.out.println("感谢您使用桌子费用估算程序!");
System.out.println("再见!");
}
private static void printReport(double area, double tableCost, int materialMenuItem) {
System.out.println("\n输出报告:");
System.out.format(Locale.ENGLISH, "桌子的面积为%.2f平方英寸\n", area);
System.out.println("桌子将使用" + getMaterialName(materialMenuItem) + "制造");
System.out.printf(Locale.ENGLISH, "桌子的费用为$%.2f\n", tableCost);
}
private static int getShapeMenuItem(Scanner scan) {
System.out.println("\n\n您想要构建什么形状的桌子?");
System.out.println("\t1. 矩形");
System.out.println("\t2. 正方形");
System.out.println("\t3. 圆形");
System.out.println("\t4. 结束程序");
System.out.print("输入菜单选项:");
return scan.nextInt();
}
private static int getMaterialMenuItem(Scanner scan) {
while (true) {
System.out.println("\n您想要使用哪种材料?");
System.out.println("\t1. 层压板(每平方英寸$0.125)");
System.out.println("\t2. 橡木(每平方英寸$0.25)");
System.out.print("输入菜单选项:");
int menu = scan.nextInt();
if (menu == 1 || menu == 2)
return menu;
System.err.println("错误 - 无效的输入。请重新输入有效值。");
}
}
private static double getArea(Scanner scan, int shapeMenuItem) {
if (shapeMenuItem == 1)
return getRectangleArea(scan);
if (shapeMenuItem == 2)
return getSquareArea(scan);
if (shapeMenuItem == 3)
return getCircleArea(scan);
return Double.NaN;
}
private static double getRectangleArea(Scanner scan) {
double length = readDouble(scan, "输入桌子的长度(英寸):");
double width = readDouble(scan, "输入桌子的宽度(英寸):");
return length * width;
}
private static double getSquareArea(Scanner scan) {
double length = readDouble(scan, "输入桌子的边长(英寸):");
return length * length;
}
private static double getCircleArea(Scanner scan) {
double diameter = readDouble(scan, "输入桌子的直径(英寸):");
double radius = diameter / 2;
return Math.PI * radius * radius;
}
private static String getMaterialName(int materialMenuItem) {
if (materialMenuItem == 1)
return "层压板";
if (materialMenuItem == 2)
return "橡木";
return null;
}
private static double getMaterialCost(int materialMenuItem) {
if (materialMenuItem == 1)
return 0.125;
if (materialMenuItem == 2)
return 0.25;
return Double.NaN;
}
private static double readDouble(Scanner scan, String msg) {
while (true) {
System.out.print(msg);
double val = scan.nextDouble();
if (val > 0)
return val;
System.err.println("错误 - 值必须大于零。请重新输入!");
}
}
}
英文:
public class Table {
public static void main(String[] args) {
System.out.println("Welcome to Tables are Us - Your One Stop Table Shop");
System.out.println("Mike - Master Table Builder");
int totalTables = 0;
double totalTableCost = 0;
Scanner scan = new Scanner(System.in);
scan.useLocale(Locale.ENGLISH);
while (true) {
int shapeMenuItem = getShapeMenuItem(scan);
if (shapeMenuItem == 4) {
printFinalResults(totalTables, totalTableCost);
return;
}
double area = getArea(scan, shapeMenuItem);
if (Double.isNaN(area))
System.err.println("Error - Invalid Entry. Please reenter a valid value.");
else {
int materialMenuItem = getMaterialMenuItem(scan);
double tableCost = area * getMaterialCost(materialMenuItem);
totalTableCost += tableCost;
totalTables++;
printReport(area, tableCost, materialMenuItem);
}
}
}
private static void printFinalResults(int numTable, double total) {
if (numTable == 0)
System.out.println("You did not estimate any tables today!");
else
System.out.format(Locale.ENGLISH, "\nThe total cost of the %d tables you estimated is $%.2f\n", numTable, total);
System.out.println("Thank you for using the table cost estimation program!");
System.out.println("GoodBye!!!");
}
private static void printReport(double area, double tableCost, int materialMenuItem) {
System.out.println("\nOutput Report:");
System.out.format(Locale.ENGLISH, "The area of table is %.2f square inches\n", area);
System.out.println("The table will be made of " + getMaterialName(materialMenuItem));
System.out.printf(Locale.ENGLISH, "The cost of the table is $%.2f\n", tableCost);
}
private static int getShapeMenuItem(Scanner scan) {
System.out.println("\n\nWhat shape of table do you wish to build?");
System.out.println("\t1. Rectangle");
System.out.println("\t2. Square");
System.out.println("\t3. Circle");
System.out.println("\t4. End Program");
System.out.print("Enter menu entry: ");
return scan.nextInt();
}
private static int getMaterialMenuItem(Scanner scan) {
while (true) {
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: ");
int menu = scan.nextInt();
if (menu == 1 || menu == 2)
return menu;
System.err.println("Error - Invalid Entry. Please reenter a valid value.");
}
}
private static double getArea(Scanner scan, int shapeMenuItem) {
if (shapeMenuItem == 1)
return getRectangleArea(scan);
if (shapeMenuItem == 2)
return getSquareArea(scan);
if (shapeMenuItem == 3)
return getCircleArea(scan);
return Double.NaN;
}
private static double getRectangleArea(Scanner scan) {
double length = readDouble(scan, "Enter the length of the table (in inches): ");
double width = readDouble(scan, "Enter the width of the table (in inches): ");
return length * width;
}
private static double getSquareArea(Scanner scan) {
double length = readDouble(scan, "Enter the length of the table (in inches): ");
return length * length;
}
private static double getCircleArea(Scanner scan) {
double diameter = readDouble(scan, "Enter the diameter of the table (in inches): ");
double radius = diameter / 2;
return Math.PI * radius * radius;
}
private static String getMaterialName(int materialMenuItem) {
if (materialMenuItem == 1)
return "Laminate";
if (materialMenuItem == 2)
return "Oak";
return null;
}
private static double getMaterialCost(int materialMenuItem) {
if (materialMenuItem == 1)
return 0.125;
if (materialMenuItem == 2)
return 0.25;
return Double.NaN;
}
private static double readDouble(Scanner scan, String msg) {
while (true) {
System.out.print(msg);
double val = scan.nextDouble();
if (val > 0)
return val;
System.err.println("Error - Value must be greater than zero. Please reenter!!");
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论