下記のプログラムのようにします。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Person {   double weight;   void eatCake(int g, int count) {     System.out.println(g + "gのケーキを"+ count +"個食べます");     weight = weight + g * count;   }   void printWeight() {     System.out.println(weight / 1000);   }   double getWeight() {     return weight;    }   void setWeight(int value) {     weight = value;   } } | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | class ChallengeP138 {   public static void main(String[] args) {     Person p1 = new Person();     Person p2 = new Person();     p1.weight = 45000;     p2.weight = 67000;     System.out.println("p1さんの体重");     p1.printWeight();     System.out.println("p2さんの体重");     p2.printWeight();     System.out.println("p1さんのおやつ");     p1.eatCake(400,4);     System.out.println("p2さんのおやつ");     for(int i = 0; i < 5; i++) {       p2.eatCake(200,1);     }      System.out.println("p1さんの体重は" + p1.getWeight() + "gです");     p1.printWeight();     System.out.println("p2さんの体重は" + p2.getWeight() + "gです");     p2.printWeight();   }     } | 
実行結果は、
p1さんの体重
45.0
p2さんの体重
67.0
p1さんのおやつ
400gのケーキを4個食べます
p2さんのおやつ
200gのケーキを1個食べます
200gのケーキを1個食べます
200gのケーキを1個食べます
200gのケーキを1個食べます
200gのケーキを1個食べます
p1さんの体重は46600.0gです
46.6
p2さんの体重は68000.0gです
68.0
となります。
 
		 
						
最近のコメント