我分配了一个编写程序,该程序使用triangleType方法,我必须编写该程序才能从用户获取三个int输入并输出三角形类型。在该方法中,我首先需要按升序对整数进行排序,以便我需要使用的比较能够正确运行。我知道我正确地执行了代码中的排序部分,因为我在开始尝试确定三角形类型之前就已经对其进行了测试。我需要使用这些比较来找到三角形类型:“如果A + B <= C,则边不代表有效的三角形。如果A = C(所有边必须具有相同的长度),则三角形为相等。如果A = B或B = C,则三角形为ISOSCELES;否则三角形为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;
}
}
if (cC > aA && cC > bB)
{
if (aA > bB)
{
a = bB;
b = aA;
c = cC;
}
else if (bB > aA)
{
a = aA;
b = bB;
c = cC;
}
}
if (a+b<=c)
{
JOptionPane.showMessageDialog(null,"Invalid Triangle");
} if (a==c) {
JOptionPane.showMessageDialog(null,"Triangle is Equilateral");
} if (a==b || b==c){
JOptionPane.showMessageDialog(null, "Triangle is Isosceles");
}
else {
JOptionPane.showMessageDialog(null,"Triangle is Scalene");
}
}
}
答案 0 :(得分:1)
您有三个不同的var listToReturn = responseList.DistinctBy(x => x.Index).ToList();
语句,分别进行评估。取而代之的是,从问题的描述来看,您似乎需要一条带有多个条件分支(即if
子句)的if
语句:
else if
答案 1 :(得分:0)
实际上有2个问题。
GlideApp.with(getContext())
.load(imgUrl)
.listener(requestListener) // log urls through listener
.into(imgAd);
这是对的,因此它显示“三角形是等边的”。然后检查:
if (a==c)
这也是对的,因此它显示“三角形是等腰三角形”。
if (a==b || b==c)
答案 2 :(得分:0)
我很困惑,为什么你不能做
if (a == b && b == c) {
JOptionPane.showMessageDialog(null,"Triangle is Equilateral");
} else if ((a == b && a != c) || (a == c && a != b) || (b == c && b != a)) {
JOptionPane.showMessageDialog(null, "Triangle is Isosceles");
} else if (a != b && a != c && b != c){
JOptionPane.showMessageDialog(null,"Triangle is Scalene");
} else {
JOptionPane.showMessageDialog(null,"Invalid Triangle");
}