第2次作业+105032014040
1.测试帖链接
http://www.cnblogs.com/LixiaZhang/p/6603016.html
2.提出的问题、发现的缺陷
1)对于负整数的输入处理部分有错误
2)代码编写要规范,比如要缩进
3)代码建议必要的注释
3.修正后的代码清单
因原先使用C++语言,所以这次改成了java语言
1 import java.util.Scanner; 2 3 public class JudgeTriangle { 4 5 public static boolean Judge(String[] str){ 6 //判断输入是否为三个字符串 7 if(str.length != 3){ 8 return false; 9 } 10 //利用正则表达式判断三个字符串是否为纯数字 11 for(int i = 0; i < 3; i++){ 12 if(str[i].matches("[0-9]+")){ 13 continue; 14 } 15 else{ 16 return false; 17 } 18 } 19 return true; 20 } 21 22 public static String triangle(int a, int b, int c){ 23 String result; 24 if (a >= 1 && a <= 100 && b >= 1 && b <= 100 && c >= 1 && c <= 100){ 25 if (a<(b + c) && b < (a + c) && c < (a + b)){ 26 if (a == b && b == c) { 27 result = "等边三角形"; 28 } 29 else if (a == b || a == c || b == c){ 30 result = "等腰三角形"; 31 } 32 else if (a * a == (b * b + c * c) || 33 b * b == (a * a + c * c) || c * c ==( a * a + b * b)){ 34 result = "直角三角形"; 35 } 36 else{ 37 result = "一般三角形"; 38 } 39 } 40 else{ 41 result = "不构成三角形"; 42 } 43 } 44 else{ 45 result = "边的值不在范围内"; 46 } 47 return result; 48 } 49 50 public static void main(String[] args){ 51 // TODO Auto-generated method stub 52 String inputStr; 53 String[] str=null; 54 Scanner scanner = new Scanner(System.in); 55 while (true) { 56 System.out.println("请输入三角形的三条边:"); 57 inputStr = scanner.nextLine(); 58 //分割成字符串数组 59 str=inputStr.split(" "); 60 //Judge方法判断输入格式 61 if(Judge(str)){ 62 //转整形数据并传入triangle方法 63 System.out.println(triangle(Integer.parseInt(str[0]), 64 Integer.parseInt(str[1]),Integer.parseInt(str[2]))); 65 }else{ 66 System.out.println("输入格式错误!"); 67 } 68 } 69 } 70 71 }
4.修正后心得体会
使用循环输入,使得可以多次输入测试。对于输入的数据不符合规范,统一提示“输入格式错误!”。
对于负数的处理错误,是因为逻辑语句的错误,现已修正。
因源代码封装性较差,所以另写了一个Judge方法进行对输入数据的判断。