A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© qxz394731688 中级黑马   /  2016-1-12 13:24  /  532 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.Scanner;

  2. class ChangFangXing {
  3.         //长方形的长
  4.         private int length;
  5.         //长方形的宽
  6.         private int width;
  7.        
  8.         public ChangFangXing(){}
  9.        
  10.         //仅仅提供setXxx()即可
  11.         public void setLength(int length) {
  12.                 this.length = length;
  13.         }
  14.        
  15.         public void setWidth(int width) {
  16.                 this.width = width;
  17.         }
  18.        
  19.         //求周长
  20.         public int getZhouChang() {
  21.                 return (length + width) * 2;
  22.         }
  23.        
  24.         //求面积
  25.         public int getArea() {
  26.                 return length * width;
  27.         }
  28. }

  29. class Test2 {
  30.         public static void main(String[] args) {
  31.                 //创建键盘录入对象
  32.                 Scanner sc = new Scanner(System.in);
  33.                
  34.                 System.out.println("请输入长方形的长:");
  35.                 int length = sc.nextInt();
  36.                 System.out.println("请输入长方形的宽:");
  37.                 int width = sc.nextInt();
  38.                
  39.                 //创建对象
  40.                 ChangFangXing cfx = new ChangFangXing();
  41.                 //先给成员变量赋值
  42.                 cfx.setLength(length);
  43.                 cfx.setWidth(width);
  44.                
  45.                 System.out.println("周长是:"+cfx.getZhouChang());
  46.                 System.out.println("面积是:"+cfx.getArea());
  47.         }
  48. }
复制代码


点评

加油  发表于 2016-1-12 21:19

7 个回复

倒序浏览
不错不错,有无考虑输入的是负值。
回复 使用道具 举报
确实,应该是做一个判断
回复 使用道具 举报
干的漂亮
回复 使用道具 举报
感觉如何呢、、、
回复 使用道具 举报
练得少,忘得快,自己是个懒人咋整
回复 使用道具 举报
好好学!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马