这是代码。当输入的所有3个边都是0时,我需要能够停止循环,而不仅仅是1或2.如果输入的边中的1或2是0,那么它应该再次提示用户。是否有操作员在满足所有3个条件时终止程序?
提前致谢!
import java.util.Scanner;
public class Program2
{
public static void main(String[] args)
{
// Read user inputs
@SuppressWarnings("resource")
Scanner key = new Scanner(System.in);
// User inputed sides of triangle
double side1, side2, side3;
// Opening messages
System.out.println("\t\t\t\t\tTriangle Identifier Program");
System.out.print("This program will tell you what type of tringle you have,");
System.out.println("\nif it is a right triangle, and the area of the triangle.");
System.out.println("Use input of 0 0 0 to terminate the program.\n");
do
{
// Get sides
System.out.print("Enter side 1 of the triangle: ");
side1 = key.nextInt();
System.out.print("Enter side 2 of the triangle: ");
side2 = key.nextInt();
System.out.print("Enter side 3 of the triangle: ");
side3 = key.nextInt();
double longest = 0;
double shorter1 = 0;
double shorter2 = 0;
if (side1 == 0 || side2 == 0 || side3 == 0)
System.out.println("These numbers do not satisfy the triangle inequality. Please try again.");
// Find the largest side
if(side1 > side2 && side1 > side3)
{
longest = side1;
shorter1 = side2;
shorter2 = side3;
}
if(side2 > side1 && side2 > side3)
{
longest = side2;
shorter1 = side1;
shorter2 = side3;
}
if(side3 > side2 && side3 > side1)
{
longest = side3;
shorter1 = side1;
shorter2 = side2;
}
if(side1 > 0 && side2 > 0 && side3 > 0)
{
if(side1==side2 && side2==side3 && side3==side1)
System.out.println("The triangle is equilateral");
if((side1==side2 && side2!=side3) || (side2==side3 && side2!=side1) || (side1==side3 && side2 !=side3))
System.out.println("The triangle is isosceles");
if(side1 != side2 && side2 != side3 && side1 != side3)
System.out.println("The triangle is scalene");
if(longest == Math.sqrt((shorter1*shorter1)+(shorter2*shorter2)))
System.out.println("The triangle is a right triangle");
else
System.out.println("The tringle is not a right triangle");
}
} while (side1 != 0 & side2 != 0 & side3 != 0);
}
}
答案 0 :(得分:0)
添加另一个if条件以检查所有3是否为0。 做 { //获得支持 System.out.print("输入三角形的第1面:"); side1 = key.nextInt(); System.out.print("输入三角形的第2面:"); side2 = key.nextInt(); System.out.print("输入三角形的第3面:"); side3 = key.nextInt();
double longest = 0;
double shorter1 = 0;
double shorter2 = 0;
if (side1 == 0 && side2 == 0 && side3 == 0){
break;
} else{
if (side1 == 0 || side2 == 0 || side3 == 0)
System.out.println("These numbers do not satisfy the triangle inequality. Please try again.");
// Find the largest side
if(side1 > side2 && side1 > side3)
{
longest = side1;
shorter1 = side2;
shorter2 = side3;
}
if(side2 > side1 && side2 > side3)
{
longest = side2;
shorter1 = side1;
shorter2 = side3;
}
if(side3 > side2 && side3 > side1)
{
longest = side3;
shorter1 = side1;
shorter2 = side2;
}
if(side1 > 0 && side2 > 0 && side3 > 0)
{
if(side1==side2 && side2==side3 && side3==side1)
System.out.println("The triangle is equilateral");
if((side1==side2 && side2!=side3) || (side2==side3 && side2!=side1) || (side1==side3 && side2 !=side3))
System.out.println("The triangle is isosceles");
if(side1 != side2 && side2 != side3 && side1 != side3)
System.out.println("The triangle is scalene");
if(longest == Math.sqrt((shorter1*shorter1)+(shorter2*shorter2)))
System.out.println("The triangle is a right triangle");
else
System.out.println("The tringle is not a right triangle");
}
}
} while (side1 != 0 & side2 != 0 & side3 != 0);