用JAVA编程求长方形和三角形的面积和周长
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用JAVA编程求长方形和三角形的面积和周长相关的知识,希望对你有一定的参考价值。
请把程序代码及运行结果截图发过来
import java.util.Scanner;
public class Test
private static double length;//边长
private static double bottom;//底
private static double tall;//高
private String perimeter;
private String area;
//无构造器
private Test()
//有构造器
private void Test(double length,double bottom,double tall)
this.length=length;
this.bottom=bottom;
this.tall=tall;
//set和get方法
public void setlength(double length)
this.length=length;
public void setbottom(double bottom)
this.bottom=bottom;
public void settall(double tall)
this.tall=tall;
public double getlength()
return this.length;
public double getbottom()
return this.bottom;
public double gettall()
return this.tall;
//周长和面积
public double getperimeter()
return length+length+bottom;
public double getarea()
return bottom*tall/2;
public static void main(String[]args)
Scanner sc = new Scanner(System.in);
System.out.println("请输入三角形的边长");
length = sc.nextDouble();
System.out.println("请输入三角形的底");
bottom = sc.nextDouble();
System.out.println("请输入三角形的高");
tall = sc.nextDouble();
System.out.println("三角形的周长是:"+new Test().getperimeter()+"三角形的面积是"+new Test().getarea());
import java.util.Scanner;
public class Triangle
/**
* 等腰
*/
public static final int ISOSCELES = 1;
/**
* 等边
*/
public static final int EQUILATERAL = 2;
/**
* 直角
*/
public static final int RIGHTANGLE = 4;
/**
* 普通
*/
public static final int GENERAL = 6;
/**
* 不构成三角形
*/
public static final int NOTHING = 7;
double a;
double b;
double c;
public Triangle ( double a, double b, double c )
this.a = a;
this.b = b;
this.c = c;
public static int isWhichOne ( Triangle triangle )
double a = triangle.a;
double b = triangle.b;
double c = triangle.c;
if (a + b > c && a + c > b && b + c > a && Math.abs (a - b) < c && Math.abs (a - c) < b && Math.abs (b - c) < a)
if (a == b && a == c)
return Triangle.EQUILATERAL | Triangle.ISOSCELES;
else if (a * a + b * b == c * c || a * a + c * c == b * b || c * c + b * b == a * a)
if (a == b || a == c || b == c)
return Triangle.RIGHTANGLE | Triangle.ISOSCELES;
return Triangle.RIGHTANGLE;
else if (a == b || a == c || b == c)
return Triangle.ISOSCELES;
else
return Triangle.GENERAL;
else
return Triangle.NOTHING;
public static double perimeter ( Triangle triangle )
return triangle.a + triangle.b + triangle.c;
public static double area ( Triangle triangle )
double a = triangle.a;
double b = triangle.b;
double c = triangle.c;
return 1.d / 4 * Math.sqrt (( a + b + c ) * ( a + b - c ) * ( a + c - b ) * ( b + c - a ));
public static void translate ( Triangle triangle )
int type = isWhichOne (triangle);
switch (type)
case Triangle.ISOSCELES:
System.out.println ("等腰三角形");
break;
case ( Triangle.EQUILATERAL | Triangle.ISOSCELES ):
System.out.println ("等边三角形 + 等腰三角形");
break;
case Triangle.GENERAL:
System.out.println ("普通三角形");
break;
case ( Triangle.RIGHTANGLE | Triangle.ISOSCELES ):
System.out.println ("直角三角形 + 等腰三角形");
break;
case Triangle.RIGHTANGLE:
System.out.println ("直角三角形");
break;
default:
System.out.println ("不构成三角形");
break;
if (type != Triangle.NOTHING)
System.out.println ("周长是:" + perimeter (triangle));
System.out.println ("面积是:" + area (triangle));
public static void main ( String[] args )
System.out.println ("输入构成三角形的三边长:(空格隔开)\\n退出按 (n/N), 否则继续: ");
String error = "输入有误,请重新输入: ";
String reg = "^\\\\d+\\\\s+\\\\d+\\\\s+\\\\d+$";
Scanner scanner = new Scanner (System.in);
while (scanner.hasNextLine ())
String line = scanner.nextLine ().trim ();
if ("n".equalsIgnoreCase (line))
break;
if (!line.matches (reg))
System.err.println (error);
else
try
String[] input = line.split ("\\\\s+");
int a = Integer.parseInt (input[0]);
int b = Integer.parseInt (input[1]);
int c = Integer.parseInt (input[2]);
Triangle triangle = new Triangle (a, b, c);
translate (triangle);
catch (NumberFormatException e)
System.err.println (error);
System.out.println ("退出按 (n/N), 否则继续: ");
scanner.close ();
python计算长方形面积周长 青少年编程电子学会python编程等级考试一级真题解析2021-6
题目名称:长方形面积周长
2021年6月 python编程等级考试一级编程题
编程实现
1、程序开始运行后,输入长方形的长(a),然后再输入长方形的宽(b)
2、程序会根据输入的数字给出长方形的面积和长方形的周长
3、输出长方形的面积和周长,并且注明是面积还是周长
输入样例
请输入长方形的长:10
请输入长方形的宽:5
输出样例
长方形的面积为:50
长方形的周长为:30
程序分析
-
利用input输入函数从键盘输入对应的三位数
-
由于input函数输入的内容是字符串,所以可以用int函数进行强制转换成整数
-
然后通过对应的长方形面积公式:长乘以宽,周长公式:2乘以(长加宽)
-
最后通过print输出函数得到结果
源程序
"""求长方形的面积与周长"""
a = int(input("
以上是关于用JAVA编程求长方形和三角形的面积和周长的主要内容,如果未能解决你的问题,请参考以下文章