我正在尝试编写一个程序,读取 3 个数字作为三角形边的长度。这些数字必须代表一个可能的三角形,如果输入 3 个零,它将终止程序。我有它的工作。它在开始时输入了 3 个零,但之后没有,即使我很确定我有一段时间检查它。同样在提供的图片中,这些值不应该起作用。
*Program Description:
*This program reads an arbitrary number of sets of triangle sides using only integer values. The progam will:
*Prompt the user for sets of numbers and process them until the user submits the numbers 0 0 0, which will terminate the program.
*For each set of three numbers, the program will print the values read.
*For each set of three numbers, the program will decide if the numbers represent the sides of a valid triangle.
*If the numbers can not represent a valid triangle, it will display an appropriate error message.
*If the numbers are valid, the program will determine, and display, the:
*side classification of the triangle – equilateral, isosceles, or scalene
*angle classification of the triangle – right, acute, or obtuse*/
import java.util.Scanner;
class triangleType
{
public static void main (String [] args)
{
int side1 = -6, side2 = -1, side3 = -1;
Scanner in = new Scanner(System.in);
System.out.println("Please enter three integers that represent VALID sides of a triangle. Enter 0 0 0 to terminate the program.");
side1 = in.nextInt();
side2 = in.nextInt();
side3 = in.nextInt();
while (side1!=0&&side2!=0&&side3!=0)//checks if the user entered 3 zeros to skip the loop and terminate the program
{
while ((side1<=0||side2<=0||side3<=0))
{
System.out.println("You have entered at least one invalid value. This means your values could not make a triangle. Please enter new values.");
side1 = in.nextInt();
side2 = in.nextInt();
side3 = in.nextInt();
}
while ((side1>=side2+side3||side2>=side1+side3||side3>=side2+side1))//checks if side values entered by user are valid
{
System.out.println("You have entered at least one invalid value. This means your values could not make a triangle. Please enter new values.");
side1 = in.nextInt();
side2 = in.nextInt();
side3 = in.nextInt();
if (side1==0&&side2==0&&side3==0)//checks if the user entered 3 zeros to break the loop and terminate the program
{
break;
}
}
}
if (side1==0&&side2==0&&side3==0)//checks if the user entered 3 zeros to terminate the program, if not the program will run
{
System.out.println("You have chosen to terminate the program.");
}
else
{
System.out.println("Your side lengths are valid. You entered: " + side1 + ", " + side2 + " and " + side3);
if((isIsosceles (side1, side2, side3)) == true)
{
System.out.println("Side classification of the triangle: Isosceles");
}
else if((isEquilateral (side1, side2, side3)) == true)
{
System.out.println("Side classification of the triangle: Equilateral");
}
else
{
System.out.println("Side classification of the triangle: Scalene");
}
if ((isAcute (side1, side2, side3)) == true)
{
System.out.println("Angle classification of the triangle: Acute");
}
else
{
System.out.println("Angle classification of the triangle: Obtuse");
}
}
}
public static boolean isEquilateral (int s1, int s2, int s3)/*takes inputs: side 1, 2 and 3 as integers. Checks to see if this triangle is equilateral
Returns boolean result of whether or not the triangle is equilateral.*/
{
if ((s1==s2)&&(s2==s3))//checks to see if all sides are equal, if so it will run and return true if not it will return false
{
return true;
}
else
{
return false;
}
}
public static boolean isIsosceles (int s1, int s2, int s3)/*takes inputs: side 1, 2 and 3 as integers. Checks to see if this triangle is isosceles
Returns boolean result of whether or not the triangle is isosceles.*/
{
if (((s1==s2)&&s1!=s3)||((s1==s3)&&s1!=s2)||((s2==s3)&&s2!=s1))//checks to see if two sides are equal, if so it will run and return true if not it will return false
{
return true;
}
else
{
return false;
}
}
public static boolean isScalene (int s1, int s2, int s3)/*takes inputs: side 1, 2 and 3 as integers. Checks to see if this triangle is scalene
Returns boolean result of whether or not the triangle is scalene.*/
{
if ((s1!=s2)&&(s1!=s3)&&(s2!=s3))//checks to see if all sides are not equal, if so it will run and return true if not it will return false
{
return true;
}
else
{
return false;
}
}
public static boolean isAcute (int s1, int s2, int s3)/*takes inputs: side 1, 2 and 3 as integers. Checks to see if this triangle is acute
Returns boolean result of whether or not the triangle is acute.*/
{
int sqrOne = 0, sqrTwo = 0;
if (s1<s3&&s2<s3)//checks to see if the lengths of side 1 and 2 are less than side 3
{
sqrOne = (s1*s1)+(s2*s2);
sqrTwo = s3*s3;
if (sqrOne>sqrTwo)
{
return true;
}
else
{
return false;
}
}
else if (s1<s2&&s3<s2)//checks to see if the lengths of side 1 and 3 are less than side 2
{
sqrOne = (s1*s1)+(s3*s3);
sqrTwo = s2*s2;
if (sqrOne>sqrTwo)
{
return true;
}
else
{
return false;
}
}
else if (s2<s1&&s3<s1)//checks to see if the lengths of side 2 and 3 are less than side 1`
{
sqrOne = (s2*s2)+(s3*s3);
sqrTwo = s1*s1;
if (sqrOne>sqrTwo)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public static boolean isObtuse (int s1, int s2, int s3)/*takes inputs: side 1, 2 and 3 as integers. Checks to see if this triangle is obtuse
Returns boolean result of whether or not the triangle is obtuse.*/
{
int sqrOne = 0, sqrTwo = 0;
if (s1<s3&&s2<s3)//checks to see if the lengths of side 1 and 2 are less than side 3
{
sqrOne = (s1*s1)+(s2*s2);
sqrTwo = s3*s3;
if (sqrOne<sqrTwo)
{
return true;
}
else
{
return false;
}
}
else if (s1<s2&&s3<s2)//checks to see if the lengths of side 1 and 3 are less than side 2
{
sqrOne = (s1*s1)+(s3*s3);
sqrTwo = s2*s2;
if (sqrOne<sqrTwo)
{
return true;
}
else
{
return false;
}
}
else if (s2<s1&&s3<s1)//checks to see if the lengths of side 2 and 3 are less than side 1
{
sqrOne = (s2*s2)+(s3*s3);
sqrTwo = s1*s1;
if (sqrOne<sqrTwo)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}