import java.util.Scanner;
public class Triangle {
// inputing the intgers of the triangle
public void inputTriangleSides()
{
System.out.println("Enter your triangle side lengths");
Scanner keyboard = new Scanner(System.in);
int side1;
int side2;
int side3;
System.out.println("Enter three integers to form a triangle:");
side1 = keyboard.nextInt();
side2 = keyboard.nextInt();
side3 = keyboard.nextInt();
}
// determines if the three number make a triangle
public void ifTriangle (int side1, int side2, int side3)
{
if (side1+side2> side3 && side1+side3>side2 && side2+side3>side1)
{
System.out.println("it a triangle");
}
else
{
System.out.println ("Not a Triangle");
}
}
// sorts the numbers from lowest to highest
public static void displaySortedNumbers(int side1, int side2, int side3 ){
if ((side1 < side2) && (side2 < side3) &&(side1 <side3)){
System.out.println("The sorted numbers are " + side1 + " " + side2 + " " + side3);
}
if ((side1 < side2) && (side2 > side3)){
System.out.println("The sorted numbers are " + side1 + " " + side3 + " " + side2);
}
if ((side1 > side2) && (side2 > side3)){
System.out.println("The sorted numbers are " + side3 + " " + side2 + " " + side1);
}
if ((side1 < side2) && (side2 > side3)){
System.out.println("The sorted numbers are " + side3 + " " + side1 + " " + side2);
}
if ((side1 > side2) && (side2 > side3) && (side1>side3)){
System.out.println("The sorted numbers are " + side2 + " " + side3 + " " + side1);
}
if ((side1 > side2) && (side2 < side3)){
System.out.println("The sorted numbers are " + side2 + " " + side1 + " " + side3);
}
}
//determines the type of triangle the three intgers form
public void displayType(int side1, int side2,int side3)
{
if(( side1+side2 > side3))
if(( side1==side2) && (side2==side3))//tell if equalateral
{
System.out.println("Equalateral Triangle.");
}
else if (( side1==side2) & (side2 != side3) || (side1 == side3) & (side3 !=side1))//tells if isosceles
{
System.out.println ("Isosceles Triangle.");
}
else if ((side1 != side2) & (side2 != side3))//tells if scalene
{
System.out.println("Scalene Triangle.");
}
else
{
System.out.println("Not a triangle");
}
}//missing return statement
// determines the perimeter
public int perimeter (int side1,int side2, int side3)
{
if (side1+side2> side3 && side1+side3>side2 && side2+side3>side1)
{
int perimeter= side1+side2+side3;
return perimeter;
}
else
return 0;
}
我试图将此代码中的方法转移到另一个方法中。
import java.util.Scanner;
public class TriangleDriver {
public static void main (String[] args) {
Triangle yourTriangle= new Triangle();
yourTriangle.inputTriangleSides();
yourTriangle.displaySortedNumbers();
yourTriangle.displayType();
yourTriangle.perimeter();
}
}
我得到了代码的displayedSortedNumber部分错误.Method显示类Triangle中的sortSortedNumber不能应用于给定的类型; required int,int,int;没有争论的原因:实际和正式的争论列表长度不同。我该如何解决这个问题?我正在尝试输出一个输出,你输入三个数字,它会对数字进行排序,看看它们是否形成一个三角形,它是什么类型并获得周长。
答案 0 :(得分:2)
从inputTriangleSides方法调用displaySortedNumbers方法。 你的主要功能将是这样的
public static void main(String args[]){
Triangle yourTriangle= new Triangle();
yourTriangle.inputTriangleSides();
}
你的输入三角形方法将是这样的
public void inputTriangleSides()
{
System.out.println("Enter your triangle side lengths");
Scanner keyboard = new Scanner(System.in);
int side1;
int side2;
int side3;
System.out.println("Enter three integers to form a triangle:");
side1 = keyboard.nextInt();
side2 = keyboard.nextInt();
side3 = keyboard.nextInt();
yourTriangle.displaySortedNumbers(side1,side2,side3);
yourTriangle.displayType(side1,side2,side3);
yourTriangle.perimeter(side1,side2,side3);
}