Как я могу l oop эту структуру решения? - PullRequest
1 голос
/ 11 февраля 2020

Извините, мне трудно получить этот l oop, и я решил перестать его подправлять и спросить ТАК. Я хочу, чтобы структура принятия решения была l oop, чтобы остановить цикл, введя «4», и я хочу, чтобы подсказка «Сделать выбор» выводилась каждый раз. Спасибо!

public class HW1Geo {
    public static void main(String[] args) {

        // This program was created in order to allow the user to calculate the areas of rectangles, triangles, and circles.

        System.out.println("Thank you for using the MCCH GeoCal program. \nWith this program, you will be able to find the area of three kinds of shapes.\nThis program uses doubles.");
        System.out.println();

        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please make a selection:");
        System.out.println("1 - Area of a rectangle");
        System.out.println("2 - Area of a triangle");
        System.out.println("3 - Area of a circle");
        System.out.println("4 - End program");
        int select = keyboard.nextInt();

        // This decision structure with a nested loop will allow the user to continue to make selections until they decide to quite the program.

        while(select != 4) {

            if(select == 1) {
                System.out.println("Enter the length of the rectangle.");
                double length = keyboard.nextDouble();
                System.out.println("Enter the width of the rectangle");
                double width = keyboard.nextDouble();
                System.out.println("The area of the rectangle is: " + rectArea(length, width));
                break;
            }
            else if(select == 2) {
                System.out.println("Enter the height of the triangle.");
                double height = keyboard.nextDouble();
                System.out.println("Enter the size of the base of the triangle.");
                double base = keyboard.nextDouble();
                System.out.println("The area of the triangle is: " + triArea(height, base));
                break;
            }
            else if(select == 3) {
                System.out.println("Enter the radius of the circle.");
                double radius = keyboard.nextDouble();
                System.out.println("The area of the circle is: " + cirArea(radius));
                break;
            }
            else if(select != 1 && select != 2 && select != 3 && select != 4) {
                System.out.println("Incorrect input.");
                break;
            }

        }
        System.out.println("Thank you for using this program.");
    }

    // This method is used to find the area of a rectangle.
    public static double rectArea(double length, double width) {
        double area = length * width;
        return area;    
    }
    public static double triArea(double height, double base) {
        double area = 0.5 * base * height;
        return area;
    }
    public static double cirArea(double radius) {
        double area = Math.PI * Math.pow(radius, 2);
        return area;
    }
}

Ответы [ 2 ]

0 голосов
/ 11 февраля 2020

Проблема с вашим кодом - это обновление select, я имею в виду отсутствие обновления.

Вы можете использовать do-while вместо while, вот модифицированная версия вашего кода:

public class Main {
        public static void main(String... args) {

            System.out.println("Thank you for using the MCCH GeoCal program. \nWith this program, you will be able to find the area of three kinds of shapes.\nThis program uses doubles.");
            System.out.println();

            Scanner keyboard = new Scanner(System.in);
            int select = 0;

            do {
                System.out.println("Please make a selection:");
                System.out.println("1 - Area of a rectangle");
                System.out.println("2 - Area of a triangle");
                System.out.println("3 - Area of a circle");
                System.out.println("4 - End program");

                select = keyboard.nextInt();
                if (select == 1) {
                    System.out.println("Enter the length of the rectangle.");
                    double length = keyboard.nextDouble();
                    System.out.println("Enter the width of the rectangle");
                    double width = keyboard.nextDouble();
                    System.out.println("The area of the rectangle is: " + rectArea(length, width));
                } else if (select == 2) {
                    System.out.println("Enter the height of the triangle.");
                    double height = keyboard.nextDouble();
                    System.out.println("Enter the size of the base of the triangle.");
                    double base = keyboard.nextDouble();
                    System.out.println("The area of the triangle is: " + triArea(height, base));
                } else if (select == 3) {
                    System.out.println("Enter the radius of the circle.");
                    double radius = keyboard.nextDouble();
                    System.out.println("The area of the circle is: " + cirArea(radius));
                } else if (select < 1 || select > 4) {
                    System.out.println("Incorrect input.");
                }

            } while (select != 4);
            System.out.println("Thank you for using this program.");
        }

        // This method is used to find the area of a rectangle.
        private static double rectArea(double length, double width) {
            return length * width;
        }

        private static double triArea(double height, double base) {
            return 0.5 * base * height;
        }

        private static double cirArea(double radius) {
            return Math.PI * Math.pow(radius, 2);
        }

    }
0 голосов
/ 11 февраля 2020

Вы можете использовать синтаксис do-while do {код wirte здесь} while (условие)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...