循环不会结束。我怎么让它退出?

时间:2014-10-08 04:07:05

标签: java methods duplicates switch-statement

之前因未指明问题而受到责骂。承诺这次做得更好。

出于某种原因,当我输入0时循环不会停止,而是继续再次打印出main方法。我该如何解决?我无法发现它有什么问题,因为其他一切都顺利进行。

另外,我想使用Switch而不是If,但我不断收到重复变量错误。为什么?

import java.util.Scanner;

public class FindArea 
{
    // side is only input provided to cube. Area of the cube is returned
    public static double cube (double side)
    {
        double Area;
        return Area = 6 * side * side; 
    }
    // radius is only input provided to sphere. Area of the sphere is returned
    public static double sphere (double radius)
    {
        double Area;
        return Area = 4 * 3.14 * radius * radius; 
    }
    // radius and height are the only inputs provided to cylinder. 
    // Area of the cylinder is returned
    public static double cylinder (double radius, double height)
    {
        double Area;
        return Area = 2 * 3.14 * radius * height + 2 * 3.14 * radius * radius; 
    }
    // outerR and innerR are the only inputs provided to doughnut. 
    // Area of the doughnut is returned
    public static double doughnut (double outerR, double innerR)
    {
        double Area;
        return Area = (2 * 3.14 * innerR) * (2 * 3.14 * outerR);
    }

    public static void main(String[] args)
    {
        int n = 4;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");

        do
        {
            n = input.nextInt();
            if ( n<0 || n>4) {
                System.out.println("Invalid");
            }

            if(n == 1) {
                System.out.print("Enter side measurement of cube: ");
                double side = input.nextDouble();
                double Area = cube(side);   
                System.out.println("The area of the cube is: " + Area);
            }
            if (n == 2) {
                System.out.print("Enter radius measurement of sphere: ");
                double radius = input.nextDouble();
                double Area = sphere (radius);
                System.out.println("The area of the sphere is: " + Area); 
            }
            if (n == 3) {
                System.out.print("Enter radius measurement of cylinder: ");
                double radius = input.nextDouble();
                System.out.print("Enter height measurement of cylinder: ");
                double height = input.nextDouble();
                double Area = cylinder (radius, height);
                System.out.println("The area of the cylinder is: " + Area);
            } 
            if (n == 4) {
                System.out.print("Enter inner radius: ");
                double innerR = input.nextDouble();
                System.out.print("Enter outer radius: ");
                double outerR = input.nextDouble();
                double Area = doughnut (outerR, innerR);
                System.out.println("The area of the donut is: " + Area);
            }
            System.out.println("--------");
            System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
            System.out.println("Or enter 0 to exit");
        } while (n != 0);
        System.exit(1);
    }
}

3 个答案:

答案 0 :(得分:2)

它再次打印输出,因为直到Do-While循环结束才进行比较。

它会检查除0以外的所有n个案例,并打印输出,无论如何。

我认为你所寻求的更是如此:

n = input.nextInt();
        if(n!=0){
            if ( n<0 || n>4) {
                System.out.println("Invalid");
            }

            if(n == 1) {
                System.out.print("Enter side measurement of cube: ");
                double side = input.nextDouble();
                double Area = cube(side);   
                System.out.println("The area of the cube is: " + Area);
            }
            if (n == 2) {
                System.out.print("Enter radius measurement of sphere: ");
                double radius = input.nextDouble();
                double Area = sphere (radius);
                System.out.println("The area of the sphere is: " + Area); 
            }
            if (n == 3) {
                System.out.print("Enter radius measurement of cylinder: ");
                double radius = input.nextDouble();
                System.out.print("Enter height measurement of cylinder: ");
                double height = input.nextDouble();
                double Area = cylinder (radius, height);
                System.out.println("The area of the cylinder is: " + Area);
            } 
            if (n == 4) {
                System.out.print("Enter inner radius: ");
                double innerR = input.nextDouble();
                System.out.print("Enter outer radius: ");
                double outerR = input.nextDouble();
                double Area = doughnut (outerR, innerR);
                System.out.println("The area of the donut is: " + Area);
            }
            System.out.println("--------");
            System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
            System.out.println("Or enter 0 to exit");
        }

请注意n!=0

另外看看switch-case的使用,一旦你学会了这些比较,这些比较不仅会更快,而且很容易理解

答案 1 :(得分:0)

您可以尝试使用开关盒 -

public static void main(String[] args)
    {
        int n = 4;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
        System.out.println("Or enter 0 to exit");

        n = input.nextInt();
    if ( n<0 || n>4) {
        System.out.println("Invalid");
    }
    else{
        switch(n){
        case 0:{
            System.out.println("You enter 0 to exit");
            break;
        }
        case 1:{
             System.out.print("Enter side measurement of cube: ");
             double side = input.nextDouble();
             double Area = cube(side);   
             System.out.println("The area of the cube is: " + Area);
            break;
        }
        case 2:{
            System.out.print("Enter radius measurement of sphere: ");
            double radius = input.nextDouble();
            double Area = sphere (radius);
            System.out.println("The area of the sphere is: " + Area); 
            break;
        }
        case 3: {
            System.out.print("Enter radius measurement of cylinder: ");
            double radius = input.nextDouble();
            System.out.print("Enter height measurement of cylinder: ");
            double height = input.nextDouble();
            double Area = cylinder (radius, height);
            System.out.println("The area of the cylinder is: " + Area);
            break;
        }
        case 4:{
            System.out.print("Enter inner radius: ");
            double innerR = input.nextDouble();
            System.out.print("Enter outer radius: ");
            double outerR = input.nextDouble();
            double Area = doughnut (outerR, innerR);
            System.out.println("The area of the donut is: " + Area);
            break;
        }

        }

    }

}

答案 2 :(得分:-1)

我使用开关盒完成了这个。非常简单

public class FindArea {
        // side is only input provided to cube. Area of the cube is returned
        public static double cube(double side) {
            double Area;
            return Area = 6 * side * side;
        }

        // radius is only input provided to sphere. Area of the sphere is returned
        public static double sphere(double radius) {
            double Area;
            return Area = 4 * 3.14 * radius * radius;
        }

        // radius and height are the only inputs provided to cylinder.
        // Area of the cylinder is returned
        public static double cylinder(double radius, double height) {
            double Area;
            return Area = 2 * 3.14 * radius * height + 2 * 3.14 * radius * radius;
        }

        // outerR and innerR are the only inputs provided to doughnut.
        // Area of the doughnut is returned
        public static double doughnut(double outerR, double innerR) {
            double Area;
            return Area = (2 * 3.14 * innerR) * (2 * 3.14 * outerR);
        }

        public static void main(String[] args) {

            Scanner input = new Scanner(System.in);
            System.out
                    .println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut, Or enter 0 to exit ");



            int n = 0;
            double side = 0;
            double Area = 0;
            double radius = 0;
            double height = 0;

            n = input.nextInt();
            switch (n) {

            case 0:
        System.out.println("Exit");
        System.exit(1);
        break;
            case 1:
                System.out.print("Enter side measurement of cube: ");
                side = input.nextDouble();
                Area = cube(side);
                System.out.println("The area of the cube is: " + Area);
                break;

            case 2:
                System.out.print("Enter radius measurement of sphere: ");
                radius = input.nextDouble();
                Area = sphere(radius);
                System.out.println("The area of the sphere is: " + Area);
                break;

            case 3:
                System.out.print("Enter radius measurement of cylinder: ");
                radius = input.nextDouble();
                System.out.print("Enter height measurement of cylinder: ");
                height = input.nextDouble();
                Area = cylinder(radius, height);
                System.out.println("The area of the cylinder is: " + Area);
                break;

            case 4:
                System.out.print("Enter inner radius: ");
                double innerR = input.nextDouble();
                System.out.print("Enter outer radius: ");
                double outerR = input.nextDouble();
                Area = doughnut(outerR, innerR);
                System.out.println("The area of the donut is: " + Area);
                break;

            default:
                System.out.println("--------");
                System.out.println("Invalid option selected");
                System.out.println("--------");
                System.out
                        .println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
                System.out.println("Or enter 0 to exit");
                n = input.nextInt();
                break;
            }
        }
    }
祝你好运。