Java 程序用于查找矩形的周长
javaobject oriented programmingprogramming更新于 2024/4/12 9:16:17
在本文中,我们将了解如何查找矩形的周长。矩形的周长是通过将矩形所有边的长度相加来计算的。
下面是矩形的示例。矩形的周长是矩形的两个长度和两个宽度的总长度减去 −

输入
假设我们的输入是 −
矩形的边长为:5、8、5、8
输出
所需的输出将是 −
周长:26
算法
步骤 1 - 开始 步骤 2 - 声明 5 个浮点变量 a、b、c、d 和周长 步骤 3 - 从用户处读取 a 和 b 的值,因为矩形的对边相等, 只需输入两条边就足够了。 步骤 4- 通过将所有边相加来计算周长 步骤 5- 显示周长值 步骤 6 - 停止
示例 1
import java.util.Scanner;
public class RectanglePerimeter{
public static void main (String args[]){
float a ,b, c, d, perimeter;
System.out.println("Required packages have been imported");
Scanner my_scanner = new Scanner(System.in);
System.out.println("A Scanner object has been defined ");
System.out.print("Enter the length of first side : ");
a = my_scanner.nextFloat();
System.out.print("Enter the length of second side : ");
b = my_scanner.nextFloat();
c = a;
d = b;
System.out.printf("The length of the sides of the Rectangle are %.2f %.2f %.2f %.2f", a, b, c, d);
perimeter = a + b + c + d;
System.out.printf("\nThe perimeter of Rectangle is: %.2f",perimeter);
}
}
输出
Required packages have been imported A Scanner object has been defined Enter the length of first side : 5 Enter the length of second side : 8 The length of the sides of the Rectangle are 5.00 8.00 5.00 8.00 The perimeter of Rectangle is: 26.00
示例 2
这里,整数已经被预先定义,并且它的值被访问并显示在控制台上。
public class RectanglePerimeter{
public static void main (String args[]){
float a ,b, c, d, perimeter;
a=c= 8;
b=d= 5;
System.out.printf("The length of the sides of the Rectangle are %.2f %.2f %.2f %.2f", a, b, c, d);
perimeter = a + b + c + d;
System.out.printf("\nThe perimeter of Rectangle is: %.2f",perimeter);
}
}
输出
The length of the sides of the Rectangle are 8.00 5.00 8.00 5.00 The perimeter of Rectangle is: 26.00
相关文章
有用资源
java 参考教程 - 该教程包含有关 java 的更多信息:https://www.cainiaomax.com/java/

