图形的识别,创建一个父类,拥有可以识别图形的方法(长方形,正方形,三角形,直角三角形)。
提示:根据边长和边数判断。
https://blog.csdn.net/wsy897/article/details/80106386
基本思路:
1、定义类,包含一个边数,一个数组存储每条边的长度,一个方法,判断图形类型。
2、主方法,接收用户的输入。参考 demo:
import java.util.Scanner;
public class GraphIdentify {
int vertextCount;
double [] edges ;
public GraphIdentify(int vertextCount,double[] edges) {
this.vertextCount = vertextCount;
this.edges = edges;
}
/**
* 判断图形类型
*/
public void identify() {
switch(vertextCount) {
case 3://三角形 校验边
break;
case 4://正方形,校验四条表相同,长方形,两条边相同
break;
case 5:
break;
default://其他图形
System.out.println("其他边形");
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入图形的边数");
//输入边数
int vertexCount = scanner.nextInt();
double [] edges = new double[vertexCount];
//循环输入每条边的长度
int count =0;
while(count<vertexCount) {
edges[count++] = scanner.nextDouble();
}
//创建一个对象
GraphIdentify graphIdentify = new GraphIdentify(vertexCount,edges);
//调用识别方法
graphIdentify.identify();
scanner.close();
}
}