JAVA判断是否组成三角形 |
您所在的位置:网站首页 › java判断三角形的类型 › JAVA判断是否组成三角形 |
下面的代码不知哪里错了,无论输入什么数,结果都是“不能构成三角形”importjava.util.Scanner;/***判断是否构成三角形,构成何种三角形*/classJudge{publicbooleanisTriangle(inta,... 下面的代码不知哪里错了,无论输入什么数,结果都是“不能构成三角形” import java.util.Scanner; /** * 判断是否构成三角形,构成何种三角形 */ class Judge { public boolean isTriangle(int a, int b, int c) { boolean flag = false; if ((a + b > c) && (a + c > b) && (b + c > a)) { flag = true; } return flag; } public String shape(int a, int b, int c) { String shape = ""; if (a == b && b == c) { shape = "这是一个等边三角形"; } else if ((a * a + b * b == c * c) || (a * a + c * c == b * b) || (c * c + b * b == a * a)) { shape = "这是一个直角三角形"; } else if ((a * a > b * b + c * c) || (b * b > c * c + a * a) || (c * c > b * b + a * a)) { shape = "这是一个钝角三角形"; } else { shape = "这是一个锐角三角形"; } return shape; } } public class Shape { public static void main(String[] args) { Scanner input = new Scanner(System.in); String d = "y"; while ("y".equals(d)) { System.out.print("请输入第一边:"); int a = input.nextInt(); System.out.print("请输入第二边:"); int b = input.nextInt(); System.out.print("请输入第三边:"); int c = input.nextInt(); Judge show = new Judge(); if ("true".equals(show.isTriangle(a, b, c))) { show.shape(a, b, c); } else { System.out.println("这不能构成三角形"); } System.out.print("继续吗?(y/n):"); d = input.next(); } System.out.println("谢谢使用!"); } } 展开 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |