前提・実現したいこと
四角形の大きさと円の半径を表示し、面積を出力したい。
発生している問題・エラーメッセージ
My.java:41: エラー: staticでない変数 thisをstaticコンテキストから参照することはできません Rectangle r = new Rectangle(); ^ My.java:42: エラー: シンボルを見つけられません r.width = 10; ^ シンボル: 変数 width 場所: タイプMy.Rectangleの変数 r My.java:43: エラー: シンボルを見つけられません r.height = 20; ^ シンボル: 変数 height 場所: タイプMy.Rectangleの変数 r My.java:44: エラー: シンボルを見つけられません System.out.println("r.width = "+r.width); ^ シンボル: 変数 width 場所: タイプMy.Rectangleの変数 r My.java:45: エラー: シンボルを見つけられません System.out.println("r.hight ="+r.height); ^ シンボル: 変数 height 場所: タイプMy.Rectangleの変数 r My.java:48: エラー: staticでない変数 thisをstaticコンテキストから参照することはできません Circle c = new Circle(); ^ My.java:50: エラー: シンボルを見つけられません System.out.println("c.radius = " + c.radius); ^ シンボル: 変数 radius 場所: タイプMy.Circleの変数 c
該当のソースコード
java
1public class My{ 2 3 int radius; 4 int width; 5 int height; 6 7 public class Rectangle{ 8 9 Rectangle(int w, int h){ 10 setSize(w,h); 11 } 12 13 void setSize(int w, int h) { 14 width = w; 15 height = h; 16 } 17 18 int getArea() { 19 return width * height; 20 } 21 } 22 23 public class Circle{ 24 25 Circle(int r){ 26 setRadius(r); 27 } 28 29 void setRadius(int r){ 30 radius = r; 31 } 32 double getCiArea(){ 33 return 3.14 * radius * radius; 34 35 } 36 } 37 38 public static void main(String[] args){ 39 Rectangle r = new Rectangle(); 40 r.width = 10; 41 r.height = 20; 42 System.out.println("r.width = "+r.width); 43 System.out.println("r.hight ="+r.height); 44 System.out.println("r.getArea = "+r.getArea()); 45 46 Circle c = new Circle(); 47 c.setRadius(10); 48 System.out.println("c.radius = " + c.radius); 49 System.out.println("c.getArea() = " + c.getCiArea()); 50 } 51}
回答2件
あなたの回答
tips
プレビュー