编写 Java 程序,通过示例查找矩形的面积和周长。
- 如果我们知道宽度和高度,我们可以使用以下公式计算矩形的面积:面积 = 宽度 * 高度。
- 周长是边缘的距离。我们可以使用以下公式计算矩形的周长:周长 = 2 * (宽度 + 高度)
Java 程序查找矩形的面积和周长
此程序允许用户输入矩形的宽度和高度。通过使用这些值,此程序将计算矩形的面积和矩形的周长。
package Area;
import java.util.Scanner;
public class AreaOfRctangle {
private static Scanner sc;
public static void main(String[] args) {
double width, height, Area, Perimeter;
sc = new Scanner(System.in);
System.out.println(" Please Enter the Width of a Rectangle = ");
width = sc.nextDouble();
System.out.println(" Please Enter the Height of a Rectangle = ");
height = sc.nextDouble();
Area = width * height;
Perimeter = 2 * (width + height);
System.out.format(" The Area of a Rectangle = %.2f\n",Area);
System.out.format(" The Perimeter of a Rectangle = %.2f\n", Perimeter);
}
}

在查找矩形面积的 Java 程序中,以下语句允许用户输入宽度和高度。然后,我们将这些值赋给已声明的变量 Width 和 Height。
提示:您可以删除第二个 Java System.out.println 语句以缩短代码。
System.out.println(" Please Enter the Width of a Rectangle = ");
width = sc.nextDouble();
System.out.println(" Please Enter the Height of a Rectangle = ");
height = sc.nextDouble();
接下来,我们将使用数学公式计算矩形的面积。
Area = width * height;
下一行,我们将计算矩形的周长
Perimeter = 2 * (width + height);
以下 System.out.format 语句帮助我们打印矩形的周长和面积
Java 程序使用函数查找矩形面积
此程序使用了我们在第一个示例中指定的逻辑。但我们将矩形面积的逻辑分开,并将其放在一个方法中。
package Area;
import java.util.Scanner;
public class AreaOfRctangleUsingMethods {
private static Scanner sc;
public static void main(String[] args) {
double width, height;
sc = new Scanner(System.in);
System.out.println("\n Please Enter the Width = ");
width = sc.nextDouble();
System.out.println("\n Please Enter the Height = ");
height = sc.nextDouble();
AreaofRectangle(width, height);
}
public static void AreaofRectangle( double width, double height ) {
double Area, Perimeter;
Area = width * height;
Perimeter = 2 * (width + height);
System.out.format("\n The Area of a Rectangle = %.2f\n",Area);
System.out.format("\n The Perimeter of a Rectangle = %.2f\n", Perimeter);
}
}
输出
Please Enter the Width =
7
Please Enter the Height =
8
The Area of a Rectangle = 56.00
The Perimeter of a Rectangle = 30.00
Java 程序使用面向对象编程查找矩形面积
在此 Java 程序示例中,我们将代码按面向对象编程进行划分。为此,我们首先创建一个包含方法的类。
提示:通常,您不必编写第一个方法。我们使用此方法来显示可用选项。
package Area;
public class AreaOfaRectangle {
double Area, Perimeter;
public void AreaofRectangle( double width, double height ) {
Area = width * height;
Perimeter = 2 * (width + height);
System.out.format(" The Area of Rectangle = %.2f\n", Area);
System.out.format(" The Perimeter of Rectangle = %.2f\n", Perimeter);
}
public double RectangleArea( double width, double height ) {
Area = width * height;
return Area;
}
}
在主 Java 程序中,我们将创建上述类的实例并调用方法。
package Area;
import java.util.Scanner;
public class AreaOfRectangleUsingClass {
private static Scanner sc;
public static void main(String[] args) {
double width, height, Area;
sc = new Scanner(System.in);
System.out.println(" Please Enter the Width = ");
width = sc.nextDouble();
System.out.println(" Please Enter the Height = ");
height = sc.nextDouble();
AreaOfaRectangle ar = new AreaOfaRectangle();
ar.AreaofRectangle(width, height);
Area = ar.RectangleArea(width, height);
System.out.format("\n Second Method: The Area of Rectangle = %.2f ", Area);
}
}
Please Enter the Width =
5
Please Enter the Height =
7
The Area of a Rectangle = 35.00
The Perimeter of a Rectangle = 24.00
Second Method: The Area of Rectangle = 35.00
AreaOfARectangle 类分析
- 首先,我们声明了一个带有两个参数的 AreaofRectangle 函数。我们在此函数中使用各自的数学公式计算矩形的面积和周长。接下来,我们使用 System.out.println 语句打印输出。
- 接下来,我们声明了一个类型为 double 的 RectangleArea 函数,带有两个参数。在该函数中,它将计算矩形的面积,然后我们返回该值。
矩形面积主类分析
首先,我们创建了 AreaOfARectangle 类的实例/对象
AreaOfaRectangle ar = new AreaOfaRectangle();
接下来,我们调用 AreaofRectangle 方法。请注意,这是第一个具有 void 关键字的方法,它将从 AreaOfARectangle 类中计算面积和周长并打印输出。
ar.AreaofRectangle(width, height);
接下来,我们调用 RectangleArea 方法。第二个 double 类型的方法计算面积并返回值。因此,我们将返回值赋给 Area 变量。
Area = ar.RectangleArea(width, height);
最后,使用以下 System.out.format 语句打印由 RectangleArea 方法计算出的面积。
System.out.format("\n Second Method: The Area of Rectangle = %.2f ", Area);
Java 程序查找矩形周长
此示例允许输入矩形的宽度和高度,将两者相加再乘以二即可得到面积。因此,矩形的周长等于 2 * (宽度 + 高度)。
package Area;
import java.util.Scanner;
public class PerimOfRectangle1 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
double width, height, Perimeter;
System.out.print("Enter the Width = ");
width = sc.nextDouble();
System.out.print("Enter the Height = ");
height = sc.nextDouble();
Perimeter = 2 * (width + height);
System.out.format("The Perimeter = %.2f", Perimeter);
}
}
Enter the Width = 17
Enter the Height = 44
The Perimeter = 122.00
在此 程序中,我们声明了一个返回矩形周长的 rectanglePerimeter 函数。
package Area;
import java.util.Scanner;
public class PerimOfRectangle2 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
float width, height, Perimeter;
System.out.print("Enter the Rectangle Width = ");
width = sc.nextFloat();
System.out.print("Enter the Rectangle Height = ");
height = sc.nextFloat();
Perimeter = rectanglePerimeter(width, height);
System.out.format("The Perimeter of a Rectangle = %.2f", Perimeter);
}
public static float rectanglePerimeter(float width, float height) {
return 2 * (width + height);
}
}
