Попытка найти тип треугольника, когда пользователь вводит значение, отличное от 0, затем перейдите к области используя формулу Херона. Если введено 0, программа должна завершиться, сказав, что она недействительна, а не перейти к области.Попытка найти тип треугольника, когда пользователь вводит значение, отличное от 0, затем перейдите к области с помощью формулы Херона
import java.util.Scanner;
public class triangle
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the length of the first side: ");
int firstSide = input.nextInt();
System.out.print("Enter the length of the second side: ");
int secSide = input.nextInt();
System.out.print("Enter the length of the third side: ");
int thirdSide = input.nextInt();
if(firstSide==0||secSide==0||thirdSide==0)
System.out.println("This is not a valid triangle");
else
if(firstSide==secSide&&secSide==thirdSide&&thirdSide==firstSide)
System.out.println("This is an equilateral triangle");
else
if((firstSide==secSide)||(secSide==thirdSide)|| (thirdSide==firstSide))
System.out.println("This is an isoceles triangle");
else
if(firstSide!=secSide&&secSide!=thirdSide&&thirdSide!=firstSide)
System.out.println("This is a scalene triangle");
double s,x;
s=(firstSide+secSide+thirdSide)/2.0;
x=(s * (s-firstSide) * (s-secSide) * (s-thirdSide));
double Area = Math.sqrt(x);
System.out.print("The area is: " + Area);
}
}
sample run
Output:
Enter the length of the first side: 0;
Enter the length of the second side: 6;
Enter the length of the third side: 1029;
This is not a valid triangle.;
Enter the length of the first side: 2 ;
Enter the length of the second side: 3;
Enter the length of the third side: 4;
This is a scalene triangle.;
The area is: 2.9;
У меня нет вопроса ... Что происходит в данный момент при запуске вышеуказанного кода? – Peter
Вопрос в том, что программа должна завершиться, когда одно значение равно 0, если оно неверно, а не перейти к области. Как бы я это сделал –
как этот javascript? –