Java 编程存储数量并写入else if语句以显示正确的数量
最后的程序将显示最终估算报告。如果用户只要求一个表,输出将显示最终的总估算值,该表为单数。如果用户要求多个表,输出将显示表的数量和总成本。如果用户根本没有请求任何估算,程序应该显示一条消息,说明这一点 它只显示“您没有估计任何表…”Java 编程存储数量并写入else if语句以显示正确的数量,java,Java,最后的程序将显示最终估算报告。如果用户只要求一个表,输出将显示最终的总估算值,该表为单数。如果用户要求多个表,输出将显示表的数量和总成本。如果用户根本没有请求任何估算,程序应该显示一条消息,说明这一点 它只显示“您没有估计任何表…” 公共类表{ 公共静态void main(字符串[]args){ 弦材料; 国际选择; 国际选择; int numTable=0; 双重成本; 双倍合计=0; 双格子=0.125; 双pOak=0.25; 扫描仪输入=新扫描仪(System.in); System.o
公共类表{
公共静态void main(字符串[]args){
弦材料;
国际选择;
国际选择;
int numTable=0;
双重成本;
双倍合计=0;
双格子=0.125;
双pOak=0.25;
扫描仪输入=新扫描仪(System.in);
System.out.println(“欢迎来到Tables is Us-您的一站式餐桌店”);
System.out.println(“Mike-mastertablebuilder”);
布尔bFlag=true;
while(bFlag){
双面积=0;
双倍长度=-1;
双宽度=-1;
双直径=-1;
双半径=-1;
做{
System.out.println(“\n\n您希望构建什么形状的表?”);
System.out.println(“\t1.矩形”);
System.out.println(“\t2.Sqaure”);
System.out.println(“\t3.Circular”);
System.out.println(“\t4.End程序”);
系统输出打印(“输入菜单项:”);
tChoice=input.nextInt();
如果(t选择<1 | | t选择>4){
System.out.println(“错误-无效输入。请重新输入有效值”);
}
}而(t选择<1 | | t选择>4);
如果(tChoice==1){
while(长度<0){
System.out.print(“输入表格的长度(以英寸为单位):”;
长度=输入.nextDouble();
如果(长度<0){
System.out.println(“错误-长度必须大于零。请重新输入!!”;
}
}
而(宽度<0){
System.out.print(“输入表格的宽度(以英寸为单位):”;
宽度=输入.nextDouble();
如果(宽度<0){
System.out.println(“错误-宽度必须大于零。请重新输入!!”;
}
}
面积=长度*宽度;
}else if(tChoice==2){
while(长度<0){
System.out.print(“输入表格的长度(以英寸为单位):”;
长度=输入.nextDouble();
如果(长度<0){
System.out.println(“错误-长度必须大于零。请重新输入!!”;
}
}
面积=长度*长度;
}否则如果(t选择==3){
而(直径<0){
系统输出打印(“输入表格直径(英寸):”;
直径=输入.nextDouble();
如果(直径<0){
System.out.println(“错误-直径必须大于零。请重新输入!!”;
}
}
半径=直径/2;
面积=Math.PI*半径*半径;
}else if(tChoice==4){
如果(numTable==0){
System.out.println(“\n您今天没有估计任何表!”);
}否则{
System.out.printf(“您估计的“+numTable+”表的总成本为$”+String.format(“%.2f”,total));
}
System.out.println(“感谢您使用表成本估算程序!”);
System.out.println(“再见!!!”;
input.close();
返回;
}
做{
System.out.println(“\n要使用哪种类型的材质?”);
System.out.println(“\t1.层压板($0.125/平方英寸)”);
System.out.println(“\t2.橡木(每平方英寸0.25美元)”;
系统输出打印(“输入菜单项:”);
mChoice=input.nextInt();
如果(mChoice<1 | | mChoice>2){
System.out.println(“错误-无效输入。请重新输入有效值”);
}
}而(mChoice<1 | | mChoice>2);
如果(mChoice==1){
材料=“层压板”;
表成本=面积*平面;
}否则{
材料=“橡木”;
表成本=面积*pOak;
}
System.out.println(“\n输出报告:”);
System.out.printf(“表格的面积为“+String.format”(%.2f),面积)+“平方英寸”);
System.out.printf(“\n表格将由”+材料组成);
System.out.printf(“\n表的成本为$”+String.format(“%.2f”,tableCost));
总计=总计+表成本;
总计++;
}
input.close();
}
}
打印格式化需要在末尾换行%n
。
确保设置了
numTable
。请显示完整的代码,并说明要传递给程序的输入、预期的输出以及实际获得的输出。也称为发布一个包含所有必要信息的帖子,以帮助您解决问题。乍一看,如果它只输出该值,那么tChoice
为4,而numTable
为0,如果您希望这些变量包含其他值,以查看它们错误地将它们带到何处,那么您可能希望使用调试器逐步完成程序
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!!");
}
}
}
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!!");
}
}
}