共回答了17个问题采纳率:82.4%
这个程序据我看是判断三角形的形状的,但是缺了第一步——判断三个边能不能构成三角形.
package test;
import java.util.Scanner;
public class Assignment5 {
//
public static void main (String[] args) {
Scanner console = new Scanner(System.in);
int num1, num2, num3;
String another;
do
{
System.out.println("Enter the sides of the triangle: ");
num1 = console.nextInt();
num2 = console.nextInt();
num3 = console.nextInt();
Triangle myTriangle = new Triangle (num1, num2, num3);
System.out.println(myTriangle.toString() + " triangle:");
if(myTriangle.is_Triangle()){
System.out.println("tcan construct thetriangle");
//check the isosceles
if (myTriangle.is_isosceles())
System.out.println("tIt is isosceles");
else
System.out.println("tIt is not isosceles");
//check the equilateral
if (myTriangle.is_equilateral())
System.out.println("tIt is equilateral");
else
System.out.println("tIt is not a equilateral");
//check the scalene
if (myTriangle.is_scalene())
System.out.println("tIt is scalene");
else
System.out.println("tIt is not scalene");
}
else
{
System.out.println("tcan not construct thetriangle");
}
System.out.println();
System.out.print("Check another Triangle (y/n)? ");
another = console.next();
} while (another.equalsIgnoreCase("y"));
}
}
Triangle.java
package test;
public class Triangle {
private int line1;
private int line2;
private int line3;
public Triangle(int num1, int num2, int num3) {
// TODO Auto-generated constructor stub
line1 = num1;
line2 = num2;
line3 = num3;
}
public boolean is_Triangle()
{
if((line1 + line2 > line3) (line1 + line3 > line2) (line3 + line2 > line1))
{
return true;
}
return false;
}
public boolean is_isosceles() {
// TODO Auto-generated method stub
if((line1 == line2) || (line1 == line3) || (line2 == line3))
{
return true;
}
return false;
}
public boolean is_equilateral() {
// TODO Auto-generated method stub
if((line1 == line2) (line2 == line3))
{
return true;
}
return false;
}
public boolean is_scalene() {
// TODO Auto-generated method stub
if((line1 != line2) (line2 != line3) (line1 != line3))
{
return true;
}
return false;
}
public String toString()
{
return "Triangle which lines is "+line1+" "+line2+" "+line3+" ";
}
}
1年前
7