2017-06-04 80 views
0

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

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(); 
    } 
} 
+2

你应该看一看只是坚持[问] – pvg

回答

-1

稍作修改你的代码,我可以输入半径。它现在全部位于一个名为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); 
      ; 

     } 
    } 
} 

测试

enter image description here

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 
+0

我才意识到似乎正在运行的窗口背后的对话框中的问题...只要你和控制台交互,我会添加一个更好的答案。 – Stef2i

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() { 

为了解决这个问题。

不过,我建议简单起见,您使用的扫描仪或对话框