本文将为大家带来“-求长方形面积”算法的一个解法与详细解释,方法不唯一,本篇文章只列举出一种方法。
一、题目
输入一个长方形的长和宽(整数),输出该长方形的周长C和面积S
运行结果:
输入:2 5
输出:C:14 S:10
先来看下运行截图:
二、代码(含详细注解)
public static void main(String[] args) {
//创建输入流
Scanner input = new Scanner(System.in);
//输入长和宽
int width = input.nextInt();
int height = input.nextInt();
//面积
int S = width*height;
//周长
int C = (width+height)*2;
//输出周长和面积
System.out.println("C:"+C+" "+"S:"+S);
//关闭输入流,释放资源
input.close();
}
三、长方形周长与面积公式
长方形的周长和面积计算公式分别为:
周长:(长+宽)×2
面积:长×宽
希望本篇文章可以给您带来收获,有兴趣的小伙伴点个关注一起学习吧