找出正方形面积的 Java 程序
给出正方形的各个边的长度为l,编写一个 Java 程序来求其面积。正方形是一个长方形,且其长和宽相同。因此,这种类型的长方形的面积为其长度的平方。
要使用 Java 计算正方形的面积,只需将给定的正方形长度乘以其自身的长度,并将结果存储在另一个变量中。

Java 程序求正方形面积
下面是一个 Java 程序,说明如何求正方形的面积
public class AreaOfSquare {
public static void main(String args[]){
int length, area;
// length of the square
length = 55;
System.out.println("Length of the square:: " + length);
// calculating area
area = length * length;
// printing the result
System.out.println("Area of the square is:: " + area);
}
}
输出
Length of the square:: 55 Area of the square is:: 3025
Java 函数求正方形面积
在这个示例中,我们借助用户定义的函数来计算正方形的面积。
public class AreaOfSquare {
// defining a method to calculate area
public static void calcAr(int l) {
int area = l * l;
System.out.println("Area of the square is:: " + area);
}
public static void main(String args[]){
int length = 33;
System.out.println("Length of the square:: " + length);
// calling the method
calcAr(length);
}
}
输出
Length of the square:: 33 Area of the square is:: 1089
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP