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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP