【问题标题】:Perimeter Calculator Java周长计算器 Java
【发布时间】:2017-06-04 01:42:43
【问题描述】:

我正在尝试编写一个能够计算圆形、三角形或矩形周长的程序。但是程序运行不正确,它只显示菜单但我无法输入任何内容。

public class GeometryDriver 
{

public static void main(String[] args) 
{


  double radius;
  double side1 = 0; 
  double side2 = 0;
  double side3 = 0; 
  double length =0;
  double width =0; 
  double perimeter = 0;
  String userChoice;

    Scanner keyboard = new Scanner(System.in);
    int option;


    System.out.println("Welcome to the Geometry Calculator!\n"
            + "In this program we will use a menu to decide what kind of shape we will create.\n"
            + "\n1.Create and Calculate Perimeter of a Circle"
            + "\n2. Create and Calculate Perimeter of a Rectangle"
            + "\n3. Create and Calculate Perimeter of a Triangle");



    String input;
    GeometricShape aShape = null;

    option = keyboard.nextInt();




    switch (option)
    {
        case 1:



            input = JOptionPane. showInputDialog(null, "Enter the radius: ");
            radius = Double.parseDouble(input);
            aShape = new GeometricShape(radius);
            perimeter = aShape.getPerimeter();


            aShape = new GeometricShape(radius);
            break;

        case 2:


            input = JOptionPane.showInputDialog(null, "Enter the length: ");

            length = Double.parseDouble(input);

            perimeter = aShape.getPerimeter();
            input = JOptionPane.showInputDialog(null, "Enter the width: ");
            width = Double.parseDouble(input);

            aShape = new GeometricShape (length, width);


            break;
        case 3:

            perimeter = aShape.getPerimeter();

            input = JOptionPane.showInputDialog(null, "Enter side 1: ");
            input = JOptionPane.showInputDialog(null, "Enter side 2: ");
            input = JOptionPane.showInputDialog(null, "Enter side 3: ");
            side1 = Double.parseDouble(input);
            side2 = Double.parseDouble(input);
            side3 = Double.parseDouble(input);

            aShape = new GeometricShape (side1, side2, side3);

            break;
        default:

                    }


      System.out.println(aShape + " has perimeter" + perimeter);
}

}

还有……

public class GeometricShape 
{
    private double side1, side2, side3;
    private double radius; 
    private double length, width; 
    private boolean triangle = false;
    private boolean rectangle = false;
    private boolean circle = false;`


    public GeometricShape(double aSide1, double aSide2, double aSide3)
    {
        side1 = aSide1;
        side2 = aSide2;
        side3 = aSide3;
        triangle = true;
    }

    public GeometricShape(double aLength, double aWidth)
    {
        length = aLength;
        width = aWidth;
        rectangle = true;
    }


    public GeometricShape(double aRadius)
    {
        radius = aRadius;
        circle = true;
    }

    public double getRadius()
    {
        return radius;
    }

    public double getSide1()
    {
        return side1;
    }

    public double getSide2()
    {
        return side2;
    }

    public double getSide3()
    {
        return side3;
    }

    public double getLength()
    {
        return length;
    }

    public double getWidth()
    {
    return width;
    }

    public void setRadius(double aRadius)
    {
        radius = aRadius;
    }

    public void setSide1(double aSide1)
    {
        side1 = aSide1;
    }

    public void setSide2(double aSide2)
    {
        side2 = aSide2;
    }

    public void setSide3(double aSide3)
    {
        side3 = aSide3;
    }

    public void setLength(double aLength)
    {
        length = aLength;
    }

    public void setWidth(double aWidth)
    {
        width = aWidth;
    }

    public double getPerimeter()
    {

        double perimeter = 0;


        if (triangle == true)
        {
            perimeter = side1 + side2 + side3; 
        }

        else if (rectangle == true)
        {
            perimeter = (2* length) + (2*width);
        }

        else if (circle == true)
        {
            perimeter = 2* Math.PI * radius; 
        }  

    }

    public String toString()
    {
        return "The perimeter is: " + this.getPerimeter(); 
    }
}

【问题讨论】:

标签: java


【解决方案1】:

新答案 好激动的消息!它确实有效!

考虑一下:

        Scanner keyboard = new Scanner(System.in);
        JOptionPane.showInputDialog(null, "Dialog Box 1: ");
        System.out.println("Write something");
        keyboard.nextInt();

它工作正常吗?但是,当您键入此内容时

        Scanner keyboard = new Scanner(System.in);
        System.out.println("Write something");
        keyboard.nextInt();
        JOptionPane.showInputDialog(null, "Dialog Box 1: ");

它似乎停止工作。我挑战你最小化控制台的窗口。在它后面,你会发现你的对话框说“DialogBox 1:”。这显然不理想,因为您的应用程序的用户将无法使用它。

因此,您可以使用的一种解决方法是将键盘输入更改为对话框,如下所示:

String displayMessage = "Welcome to the Geometry Calculator!\n"
            + "In this program we will use a menu to decide what kind of shape we will create.\n"
            + "\n1.Create and Calculate Perimeter of a Circle"
            + "\n2. Create and Calculate Perimeter of a Rectangle"
            + "\n3. Create and Calculate Perimeter of a Triangle";



    String input;
    GeometricShape aShape = null;

    option = Integer.parseInt(JOptionPane.showInputDialog(displayMessage));

更多信息 这个问题已经讨论过几次了。 JOptionPane.showMessageDialog is not showingJOptionPane and Scanner input issue。 第二个解释了一种使用方法:

SwingUtilities.invokeLater(new Runnable() {

解决问题。

但是,为了简单起见,我建议您坚持使用扫描仪或对话框

【讨论】:

    【解决方案2】:

    对您的代码稍作修改后,我可以输入一个半径。它现在都在一个名为Main.java 的文件中,并且仍然是矩形的空指针,因为宽度为空。

    import javax.swing.*;
    import java.util.Scanner;
    
    class GeometricShape {
        private double side1, side2, side3;
        private double radius;
        private double length, width;
        private boolean triangle = false;
        private boolean rectangle = false;
        private boolean circle = false;
    
        GeometricShape(double aSide1, double aSide2, double aSide3) {
            side1 = aSide1;
            side2 = aSide2;
            side3 = aSide3;
            triangle = true;
        }
    
        GeometricShape(double aLength, double aWidth) {
            length = aLength;
            width = aWidth;
            rectangle = true;
        }
    
    
        GeometricShape(double aRadius) {
            radius = aRadius;
            circle = true;
        }
    
        public double getRadius() {
            return radius;
        }
    
        public double getSide1() {
            return side1;
        }
    
        public double getSide2() {
            return side2;
        }
    
        public double getSide3() {
            return side3;
        }
    
        public double getLength() {
            return length;
        }
    
        public double getWidth() {
            return width;
        }
    
        public void setRadius(double aRadius) {
            radius = aRadius;
        }
    
        public void setSide1(double aSide1) {
            side1 = aSide1;
        }
    
        public void setSide2(double aSide2) {
            side2 = aSide2;
        }
    
        public void setSide3(double aSide3) {
            side3 = aSide3;
        }
    
        public void setLength(double aLength) {
            length = aLength;
        }
    
        public void setWidth(double aWidth) {
            width = aWidth;
        }
    
        public double getPerimeter() {
    
            double perimeter = 0;
    
    
            if (triangle == true) {
                perimeter = side1 + side2 + side3;
            } else if (rectangle == true) {
                perimeter = (2 * length) + (2 * width);
            } else if (circle == true) {
                perimeter = 2 * Math.PI * radius;
            }
            return perimeter;
    
        }
    
    
        public String toString() {
    
    
            return "The perimeter is: " + this.getPerimeter()
                    ;
        }
    
    }
    
    
    public class Main {
    
        public static void main(String[] args) {
            {
    
    
                double radius;
                double side1 = 0;
                double side2 = 0;
                double side3 = 0;
                double length = 0;
                double width = 0;
                double perimeter = 0;
                String userChoice;
    
                Scanner keyboard = new Scanner(System.in);
                int option;
    
    
                System.out.println("Welcome to the Geometry Calculator!\n"
                        + "In this program we will use a menu to decide what kind of shape we will create.\n"
                        + "\n1.Create and Calculate Perimeter of a Circle"
                        + "\n2. Create and Calculate Perimeter of a Rectangle"
                        + "\n3. Create and Calculate Perimeter of a Triangle");
    
    
                String input;
                GeometricShape aShape = null;
    
                option = keyboard.nextInt();
    
    
                switch (option) {
                    case 1:
    
    
                        input = JOptionPane.showInputDialog(null, "Enter the radius: ");
                        radius = Double.parseDouble(input);
                        aShape = new GeometricShape(radius);
                        perimeter = aShape.getPerimeter();
    
    
                        aShape = new GeometricShape(radius);
                        break;
    
                    case 2:
    
    
                        input = JOptionPane.showInputDialog(null, "Enter the length: ");
    
                        length = Double.parseDouble(input);
    
                        perimeter = aShape.getPerimeter();
                        input = JOptionPane.showInputDialog(null, "Enter the width: ");
                        width = Double.parseDouble(input);
    
                        aShape = new GeometricShape(length, width);
    
    
                        break;
                    case 3:
    
                        perimeter = aShape.getPerimeter();
    
                        input = JOptionPane.showInputDialog(null, "Enter side 1: ");
                        input = JOptionPane.showInputDialog(null, "Enter side 2: ");
                        input = JOptionPane.showInputDialog(null, "Enter side 3: ");
                        side1 = Double.parseDouble(input);
                        side2 = Double.parseDouble(input);
                        side3 = Double.parseDouble(input);
    
                        aShape = new GeometricShape(side1, side2, side3);
    
                        break;
                    default:
    
                }
    
    
                System.out.println(aShape + " has perimeter" + perimeter);
                ;
    
            }
        }
    }
    

    测试

    Welcome to the Geometry Calculator!
    In this program we will use a menu to decide what kind of shape we will create.
    
    1.Create and Calculate Perimeter of a Circle
    2. Create and Calculate Perimeter of a Rectangle
    3. Create and Calculate Perimeter of a Triangle
    1
    The perimeter is: 12.566370614359172 has perimeter12.566370614359172
    

    【讨论】:

    • 我刚刚意识到这个问题...一旦您与控制台交互,对话框就会出现在您运行它的窗口后面。我会添加一个更好的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    相关资源
    最近更新 更多