如果语句在程序中无法正常工作以确定三角形类型

我有一个任务来编写一个程序,该程序使用我必须编写的方法triangleType,以便从用户那里获取三个int输入并输出三角形类型。在该方法中,我首先需要按升序对整数进行排序,以便我需要使用的比较能够正常工作。我知道我在代码中正确地完成了排序部分,因为我在开始尝试确定三角形类型之前就对其进行了测试。我需要使用这些比较来找到三角形类型:“如果A + B<= C,那么边不代表有效的三角形。如果 A = C(所有边的长度必须相同),则三角形为等边。如果 A = B 或 B = C,则三角形为等腰;否则三角形是SCALENE“由于某些原因,我在triangleType方法末尾的if语句无法正常工作,并且我得到了各种输出,包括”无效三角形“以及其他输出,无论我输入的整数如何。


package trianglemethod;


import javax.swing.JOptionPane;



public class TriangleMethod

{



    public static void main(String[] args)

    {

        String wordaA, wordbB, wordcC, answer;


        do 

        {

           System.out.println("Please enter all 3 side lengths of the triangle in any order.");

           wordaA = JOptionPane.showInputDialog("Enter side 1:");

           wordbB = JOptionPane.showInputDialog("Enter side 2:");

           wordcC = JOptionPane.showInputDialog("Enter side 3:");

           int aA = Integer.parseInt(wordaA);

           int bB = Integer.parseInt(wordbB);

           int cC = Integer.parseInt(wordcC);

           triangleType(aA,bB,cC);

           System.out.println("Would you like to enter another triangle?");

           answer = JOptionPane.showInputDialog("Would you like to enter another triangle?");

        } while (answer.equalsIgnoreCase("yes"));


    }


    static void triangleType(int aA, int bB, int cC) {

        int a=0, b=0, c=0;


        if (aA > bB && aA > cC)

        {

            if (bB > cC)

            {

              a = cC;

              b = bB;

              c = aA;

            }

            else if (cC > bB)

            {

              a = bB;

              b = cC;

              c = aA;

            }

        }

        if (bB > aA && bB > cC)

        {

            if (aA > cC)

            {

              a = cC;

              b = aA;

              c = bB;

            }

            else if (cC > aA)

            {

              a = aA;

              b = cC;

              c = bB;

            }

        }


慕的地6264312
浏览 143回答 3
3回答

眼眸繁星

我很困惑,你为什么不能干脆做&nbsp; &nbsp; &nbsp; &nbsp; if (a == b && b == c) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null,"Triangle is Equilateral");&nbsp; &nbsp; &nbsp; &nbsp; } else if ((a == b && a != c) || (a == c && a != b) || (b == c && b != a)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, "Triangle is Isosceles");&nbsp; &nbsp; &nbsp; &nbsp; } else if (a != b && a != c && b != c){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null,"Triangle is Scalene");&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null,"Invalid Triangle");&nbsp; &nbsp; &nbsp; &nbsp; }

繁星coding

实际上有2个问题。假设 a=5、b=5 和 c=5。你的代码现在的工作方式是检查:这是真的,所以它打印“三角形是等边的”。然后它检查:这也是真的,所以它打印“三角形是等腰”。为了防止它在找到您要查找的结果后检查下一个 if 语句,后面的所有 if 语句都必须是“else if”语句。如果上面的所有 else if 语句都是 false,则最终的 “else” 代码将运行。在你的情况下,它会运行与你的第三个陈述是假的一样长。if (a==c)if (a==b || b==c)同样正如Andronicus所指出的,如果任何一侧等于另一侧,则变量a,b和c将保持为0,因为您的代码仅检查不等式这里是排序部分的工作代码:if(aA>=bB && aA>=cC){&nbsp; &nbsp; a = aA;&nbsp; &nbsp; if(bB >= cC){&nbsp; &nbsp; &nbsp; &nbsp; b = bB;&nbsp; &nbsp; &nbsp; &nbsp; c = cC;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; b = cC;&nbsp; &nbsp; &nbsp; &nbsp; c = bB;&nbsp; &nbsp; }} else if (bB >= aA&nbsp; && bB >= cC){&nbsp; &nbsp; a = bB;&nbsp; &nbsp; if(aA >= cC){&nbsp; &nbsp; &nbsp; &nbsp; b = aA;&nbsp; &nbsp; &nbsp; &nbsp; c = cC;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; b = cC;&nbsp; &nbsp; &nbsp; &nbsp; a = aA;&nbsp; &nbsp; }} else {&nbsp; &nbsp; a = cC;&nbsp; &nbsp; if(cC >= aA){&nbsp; &nbsp; &nbsp; &nbsp; b = cC;&nbsp; &nbsp; &nbsp; &nbsp; c = aA;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; b = aA;&nbsp; &nbsp; &nbsp; &nbsp; c = cC;&nbsp; &nbsp; }}

烙印99

您有三个不同的语句,分别进行评估。相反,从问题的描述来看,听起来你需要一个具有多个条件分支(即子句)的单个语句:ififelse ifif (a+b<=c){&nbsp; &nbsp; JOptionPane.showMessageDialog(null,"Invalid Triangle");} else if (a==c) {&nbsp; &nbsp; &nbsp; &nbsp;JOptionPane.showMessageDialog(null,"Triangle is Equilateral");&nbsp;} else if (a==b || b==c){&nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, "Triangle is Isosceles");} else {&nbsp; &nbsp; JOptionPane.showMessageDialog(null,"Triangle is Scalene");}&nbsp; &nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java