编程存储数量并编写else if语句以显示正确结果。

huangapple go评论61阅读模式
英文:

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(&quot;Welcome to Tables are Us - Your One Stop Table Shop&quot;);
        System.out.println(&quot;Mike - Master Table Builder&quot;);

        boolean bFlag = true;

        while (bFlag) {
            double area = 0;
            double length = -1;
            double width = -1;
            double diameter = -1;
            double radius = -1;

            do {
                System.out.println(&quot;\n\nWhat shape of table do you wish to build?&quot;);
                System.out.println(&quot;\t1. Rectangular&quot;);
                System.out.println(&quot;\t2. Sqaure&quot;);
                System.out.println(&quot;\t3. Circular&quot;);
                System.out.println(&quot;\t4. End Program&quot;);
                System.out.print(&quot;Enter menu entry: &quot;);

                tChoice = input.nextInt();

                if (tChoice &lt; 1 || tChoice &gt; 4) {
                    System.out.println(&quot;Error - Invalid Entry. Please reenter a valid value.&quot;);
                }

            } while (tChoice &lt; 1 || tChoice &gt; 4);

            if (tChoice == 1) {
                while (length &lt; 0) {
                    System.out.print(&quot;Enter the length of the table (in inches): &quot;);
                    length = input.nextDouble();

                    if (length &lt; 0) {
                        System.out.println(&quot;Error - the length must be greater than zero. Please reenter!!&quot;);
                    }
                }

                while (width &lt; 0) {
                    System.out.print(&quot;Enter the width of the table (in inches): &quot;);
                    width = input.nextDouble();

                    if (width &lt; 0) {
                        System.out.println(&quot;Error - the width must be greater than zero. Please reenter!!&quot;);
                    }
                }

                area = length * width;
            } else if (tChoice == 2) {
                while (length &lt; 0) {

                    System.out.print(&quot;Enter the length of the table (in inches): &quot;);

                    length = input.nextDouble();

                    if (length &lt; 0) {
                        System.out.println(&quot;Error - the length must be greater than zero. Please reenter!!&quot;);
                    }
                }

                area = length * length;
            } else if (tChoice == 3) {
                while (diameter &lt; 0) {
                    System.out.print(&quot;Enter the diameter of the table (in inches): &quot;);
                    diameter = input.nextDouble();

                    if (diameter &lt; 0) {
                        System.out.println(&quot;Error - the diameter must be greater than zero. Please reenter!!&quot;);
                    }
                }

                radius = diameter / 2;
                area = Math.PI * radius * radius;

            } else if (tChoice == 4) {
                if (numTable == 0) {
                    System.out.println(&quot;\nYou did not estimate any tables today!&quot;);
                } else {
                    System.out.printf(&quot;The total cost of the &quot; + numTable + &quot;tables you estimated is $&quot; + String.format(&quot;%.2f&quot;, total));
                }


                System.out.println(&quot;Thank you for using the table cost estimation program!&quot;);
                System.out.println(&quot;GoodBye!!!&quot;);

                input.close();

                return;
            }

            do {
                System.out.println(&quot;\nWhat type of material do you want to use?&quot;);
                System.out.println(&quot;\t1. Laminate ($0.125 per square inch)&quot;);
                System.out.println(&quot;\t2. Oak ($0.25 per square inch)&quot;);
                System.out.print(&quot;Enter menu entry: &quot;);

                mChoice = input.nextInt();

                if (mChoice &lt; 1 || mChoice &gt; 2) {

                    System.out.println(&quot;Error - Invalid Entry. Please reenter a valid value.&quot;);
                }

            } while (mChoice &lt; 1 || mChoice &gt; 2);


            if (mChoice == 1) {
                material = &quot;Laminate&quot;;

                tableCost = area * pLaminate;
            } else {
                material = &quot;Oak&quot;;

                tableCost = area * pOak;
            }

            System.out.println(&quot;\nOutput Report:&quot;);
            System.out.printf(&quot;The area of table is &quot; + String.format(&quot;%.2f&quot;, area) + &quot; square inches&quot;);
            System.out.printf(&quot;\nThe table will be made of &quot; + material);
            System.out.printf(&quot;\nThe cost of the table is $&quot; + String.format(&quot;%.2f&quot;, tableCost));

            total = total + tableCost;

            total++;

        }
        input.close();
    }
}

答案1

得分: 1

System.out.printf("你估计的 %d 张桌子的总费用为 $%.2f%n", numTable, total);

Print-formatted 需要在末尾加上换行符 %n
确保 numTable 已设置。

英文:
        System.out.printf(&quot;The total cost of the %d tables you estimated is $%.2f%n&quot;, 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(&quot;Welcome to Tables are Us - Your One Stop Table Shop&quot;);
System.out.println(&quot;Mike - Master Table Builder&quot;);
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(&quot;Error - Invalid Entry. Please reenter a valid value.&quot;);
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(&quot;You did not estimate any tables today!&quot;);
else
System.out.format(Locale.ENGLISH, &quot;\nThe total cost of the %d tables you estimated is $%.2f\n&quot;, numTable, total);
System.out.println(&quot;Thank you for using the table cost estimation program!&quot;);
System.out.println(&quot;GoodBye!!!&quot;);
}
private static void printReport(double area, double tableCost, int materialMenuItem) {
System.out.println(&quot;\nOutput Report:&quot;);
System.out.format(Locale.ENGLISH, &quot;The area of table is %.2f square inches\n&quot;, area);
System.out.println(&quot;The table will be made of &quot; + getMaterialName(materialMenuItem));
System.out.printf(Locale.ENGLISH, &quot;The cost of the table is $%.2f\n&quot;, tableCost);
}
private static int getShapeMenuItem(Scanner scan) {
System.out.println(&quot;\n\nWhat shape of table do you wish to build?&quot;);
System.out.println(&quot;\t1. Rectangle&quot;);
System.out.println(&quot;\t2. Square&quot;);
System.out.println(&quot;\t3. Circle&quot;);
System.out.println(&quot;\t4. End Program&quot;);
System.out.print(&quot;Enter menu entry: &quot;);
return scan.nextInt();
}
private static int getMaterialMenuItem(Scanner scan) {
while (true) {
System.out.println(&quot;\nWhat type of material do you want to use?&quot;);
System.out.println(&quot;\t1. Laminate ($0.125 per square inch)&quot;);
System.out.println(&quot;\t2. Oak ($0.25 per square inch)&quot;);
System.out.print(&quot;Enter menu entry: &quot;);
int menu = scan.nextInt();
if (menu == 1 || menu == 2)
return menu;
System.err.println(&quot;Error - Invalid Entry. Please reenter a valid value.&quot;);
}
}
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, &quot;Enter the length of the table (in inches): &quot;);
double width = readDouble(scan, &quot;Enter the width of the table (in inches): &quot;);
return length * width;
}
private static double getSquareArea(Scanner scan) {
double length = readDouble(scan, &quot;Enter the length of the table (in inches): &quot;);
return length * length;
}
private static double getCircleArea(Scanner scan) {
double diameter = readDouble(scan, &quot;Enter the diameter of the table (in inches): &quot;);
double radius = diameter / 2;
return Math.PI * radius * radius;
}
private static String getMaterialName(int materialMenuItem) {
if (materialMenuItem == 1)
return &quot;Laminate&quot;;
if (materialMenuItem == 2)
return &quot;Oak&quot;;
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 &gt; 0)
return val;
System.err.println(&quot;Error - Value must be greater than zero. Please reenter!!&quot;);
}
}
}

huangapple
  • 本文由 发表于 2020年9月26日 05:51:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64071703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定