【问题标题】:Programming about finding the triangle关于寻找三角形的编程
【发布时间】:2014-11-26 13:42:00
【问题描述】:

已解决

这是我到目前为止所做的:

class triangle
{
     public static void main (String[] args)
 {
System.out.println("Provide three side lengths - 000 to terminate.");
int a = In.getInt();
int b = In.getInt();
int c = In.getInt();

String output1 ="";
String output2 ="";


 while(a!=0 && b!=0 && c!=0)
{
   int biggest = Math.max(a, Math.max(b,c));
   int smallest = Math.min(a, Math.min(b,c));
   int middle = 0;


   if(a!=biggest && a!=smallest)
     middle = a;
   if(b!=biggest && b!=smallest)
     middle = b;
   if(c!=biggest && c!=smallest)
     middle = c;

   if(a==b && a==c && b==c)
   {
     output1 = "equilateral";
   }
   if((a==b && a!=c) || (a==c && a!=b) || (b==c && b!=a))
   {
     output1 = "isosceles";
   }
   if(a!=b && a!=c && b!=c)
   {
     output1 = "scalene";
   }

   int angle = (middle*middle) + (smallest*smallest);
   int angle2 = (biggest*biggest);

   if(angle == angle2)
   {
     output2="right";
   }
   if(angle > angle2)
   {
     output2="acute";
   }
   if(angle < angle2)
   {
     output2="obtuse";
   }

   System.out.println(output1 + " and " + output2);
   a = In.getInt();
   b = In.getInt();
   c = In.getInt();

   if(a==0 && b==0 && c==0)
   {
     System.out.println("Thanks for using the program");
     break;
  }

  }
 }
 }

我的问题是,当我输入 5,2,5 时,它应该是等腰和锐角,但它输出为等腰和钝角,而当我输入 5,5,5 时,它会出现等边和正确。唯一有效的是,如果我输入 3,5,4,它会以不等比例出现并且是正确的。我已经有一段时间了,我的数学看起来是正确的。有人可以帮忙吗?

【问题讨论】:

    标签: java while-loop


    【解决方案1】:

    不要像您那样进行比较,而是尝试使用Arrays.sort()

    while (a != 0 && b != 0 && c != 0) {
        int[] sides = new int[]{a, b, c};
        Arrays.sort(sides);
        int smallest = sides[0];
        int middle = sides[1];
        int biggest = sides[2];
    
        //...
    }
    

    更新

    好吧,使用maxmin,你可以试试这样:

    int bigger = Math.max(a, b);
    int biggest = Math.max(bigger, c);
    int smaller = Math.min(bigger, c);
    int middle, smallest;
    if (smaller >= bigger) {
        middle = smaller;
        smallest = Math.min(a, b);
    } else {
        middle = Math.min(a, b);
        smallest = smaller;
    }
    

    *请注意,我进行的排序就像足球比赛的半决赛一样。
    我希望这可以帮助你学习逻辑。 =)

    【讨论】:

      【解决方案2】:

      跟这个逻辑有关:

         if(a!=biggest && a!=smallest)
           middle = a;
         if(b!=biggest && b!=smallest)
           middle = b;
         if(c!=biggest && c!=smallest)
           middle = c;
      

      如果你有两个或多个相同的边,这将不起作用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-05
        • 2014-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多