2016-07-15 14 views
1

私はとても迷っています。私はこのプログラムを持っています。このプログラムは、半径と高さのパラメータを持つ円柱を構築することができます。次に、半径と出力面積と体積を取得および設定するためのさまざまなメソッドを呼び出すことができます。静的で非静的なメソッドが静的に使用されていることについてはエラーがなく、私のメインに何も置くことができないので、私は渡されません。私はその意味が分からない。私は実際に他の人のコードをコンパイラにコピーして、同じエラーが出ます。私はいくつかの設定を台無しにしましたか?私はこれがおそらくスタックオーバーフローのための初歩的なものであることを知っていますが、私はこの時点で絶望的です。Java - シリンダープログラム - メインを使用することができません

public class Miller_A03Q1 { 

    public static void main(String[] args) { 
     Cylinder cylinder1 = new Cylinder(1,17); 
     Cylinder cylinder2 = new Cylinder(3,8); 
     Cylinder cylinder3 = new Cylinder(2,12); 
     Cylinder cylinder4 = new Cylinder (1,14);  
    } 

    public class Cylinder{ 
     private double radius = 0.0; 
     private double height= 0.0; 
     private double area = 0.0; 
     private double volume=0.0; 
     private String shape = "cylinder"; 


     public Cylinder(double r,double h){ 
      this.radius = r; 
      System.out.print(r); 
      this.height = h; 
      System.out.print(h); 

     } 
     public double getVolume(){ 
      double volume = 3.14 * radius * radius * height; 
      return volume; 
     } 
     public double getArea(){ 
      double circumference = 3.14 * 2 * radius; 
      double circleArea = 3.14 * radius * radius; 
      double area = (2 * circleArea) + (circumference * this.height); 
      return area; 
     } 
     public double getRadius(){ 
      return this.radius; 
     } 
     public double getHeight(){ 
      return this.height; 
     } 
     public void setHeight(double h){ 
      this.height = h; 
     } 
     public void setRadius(double r){ 
      this.radius = r; 
     } 
     @Override 
     public String toString(){ 
      return this.shape + this.radius + this.height+ this.volume + this.area; 
     }    
    }  
} 
+3

は 'のpublic staticクラスシリンダー{' ....あなた 'Cylinder'をお試しください「Miller_A03Q1」の – bradimus

+1

'Miller_A03Q1'と' Cylinder'を2つの別々のクラスとして作成することもできます。 – Blobonat

答えて

2

あなたが外部クラスが必要な場合、私は知らないが、あなたはちょうどそれが私のためにコンパイルシリンダークラス内のmainメソッドを置けば....

public class Cylinder { 
    private double radius = 0.0; 
    private double height = 0.0; 
    private double area = 0.0; 
    private double volume = 0.0; 
    private String shape = "cylinder"; 

    public Cylinder(double r, double h) { 
     this.radius = r; 
     System.out.print(r); 
     this.height = h; 
     System.out.print(h); 

    } 

    public double getVolume() { 
     double volume = 3.14 * radius * radius * height; 
     return volume; 
    } 

    public double getArea() { 
     double circumference = 3.14 * 2 * radius; 
     double circleArea = 3.14 * radius * radius; 
     double area = (2 * circleArea) + (circumference * this.height); 
     return area; 
    } 

    public double getRadius() { 
     return this.radius; 
    } 

    public double getHeight() { 
     return this.height; 
    } 

    public void setHeight(double h) { 
     this.height = h; 
    } 

    public void setRadius(double r) { 
     this.radius = r; 
    } 

    @Override 
    public String toString() { 
     return this.shape + this.radius + this.height + this.volume + this.area; 

    } 

    public static void main(String[] args) { 
     Cylinder cylinder1 = new Cylinder(1, 17); 
     Cylinder cylinder2 = new Cylinder(3, 8); 
     Cylinder cylinder3 = new Cylinder(2, 12); 
     Cylinder cylinder4 = new Cylinder(1, 14); 
    } 
} 
5

内部クラスは同じようにしていますその他のメンバー(ただし、enumを除く)。 staticを明示的に宣言していない場合は表示されませんので、mainなどの静的コンテキストからはアクセスできません。長い話を短く作るために - staticとしてあなたCylinder内部クラスを宣言し、[OK]をする必要があります:あなたはそれがインスタンスを必要と定義されているよう

public class Miller_A03Q1 { 

    public static void main(String[] args) { 
     Cylinder cylinder1 = new Cylinder(1,17); 
     Cylinder cylinder2 = new Cylinder(3,8); 
     Cylinder cylinder3 = new Cylinder(2,12); 
     Cylinder cylinder4 = new Cylinder (1,14); 
    } 

    public static class Cylinder{ 
     // etc... 
+0

私はあなたを知らないが、私はあなたを愛している。私は1つの単語が私に非常に悲しみを引き起こすとは信じられません。 – rkstm

+1

@rkstmこれはコードを書くときにはかなり普通です。小さな間違いが最大の問題を引き起こします:D – Blobonat

関連する問題