2017-06-04 17 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という名前のファイル内にあり、幅がnullなので長方形のnullポインタです。

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:"が表示されます。これは明らかにあなたのアプリケーションのユーザーがそれを使用することができないので理想的ではありません。

あなたが使用できる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 showingおよびJOptionPane and Scanner input issue。この問題を解決するために

SwingUtilities.invokeLater(new Runnable() { 

: 二つ目は、これを使用する方法を説明します。しかし

は、私はあなただけ