UT源码105032014118

(1)设计三角形问题的程序

      输入三个整数a、b、c,分别作为三角形的三条边,现通过程序判断由三条边构成的三角形的类型为等边三角形、等腰三角形、一般三角形(特殊的还有直角三角形),以及不构成三角形。(等腰直角三角形,判断为等腰三角形)

     现在要求输入三个整数a、b、c,必须满足以下条件:

     条件1   1≤a≤100          条件4   a<b+ c

     条件2   1≤b≤100          条件5   b<a+ c            

     条件3   1≤c≤100          条件6   c<a+ b 

  String triangle(int a,int b,int c)    返回字符型

 程序要求:

1)先显示:“请输入三角形的三条边:”

2)只要有不满足条件1,2,3之一,就返回“边的值不在范围内!”

3)只要有不满足4,5,6之一,就返回“不构成三角形”

4)根据边的情况分别返回:“等边三角形”“等腰三角形”“直角三角形”“一般三角形”

实验代码:

package test01;
import java.util.Scanner;

public class test01 {
public static void main(String[] args){
int a,b,c;
String result=null;
boolean flag=true;
Scanner sc = new Scanner(System.in);
while(flag){
System.out.print("*********************** ");
System.out.print("请输入三角形的三条边 ");
System.out.print("第一条边:");
a=sc.nextInt();
System.out.print("第二条边:");
b=sc.nextInt();
System.out.print("第三条边:");
c=sc.nextInt();
if((a<1||a>100)||(b<1||b>100)||(c<1||c>100)){
System.out.print("Attention!边的值不在范围内!!");
}
else
{
result=triangle(a,b,c);
System.out.print(a+","+b+","+c+"可构成"+result);
System.out.print(" *********************** ");

}
System.out.print("是否要继续输入数据?1:Yes 2:No");
String temp=sc.next();
switch(temp)
{
case"1":
flag=true;
break;
case"2":
flag=false;
System.out.print("您已退出程序");
break;
}
}
}

public static String triangle(int a,int b,int c){
String sort=null;
if((a<b+c)&&( b<a+c)&&( c<a+b)){
if((a==b)||(a==c)||(b==c)){
if((a==b)&&(b==c))
sort="等边三角形";
else
sort="等腰三角形";
}
else if((a*a+b*b==c*c)||(a*a+c*c==b*b)||(b*b+c*c==a*a))
sort="直角三角形";
else
sort="一般三角形";
}
else{
sort="不能构成三角形";
}
return sort;
}
}

原文地址:https://www.cnblogs.com/cyk0674804/p/6532890.html