我没有使用多个类获得三角形程序的输出

时间:2014-02-18 23:05:45

标签: java methods printing geometry output

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;
}

public void output(int side1 , int side2,  int side3, int perimeter)
{System.out.println(perimeter);
}
}

这是放在这个班级。

import java.util.Scanner;
public class TriangleDriver {
public static void main(String args[]){
Triangle yourTriangle= new Triangle();
yourTriangle.inputTriangleSides();
}

public void inputTriangleSides()
{
    Triangle yourTriangle= new Triangle();
    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.ifTriangle(side1,side2,side3); // is it a triangle?
    yourTriangle.displaySortedNumbers(side1,side2,side3); // sorts the number
    yourTriangle.displayType(side1,side2,side3); // type of triangle
    yourTriangle.perimeter(side1,side2,side3); // type of perimeter

}
}

当我输入数字时,我没有得到输出本应该是方法所需的打印语句,我到底错过了什么?还有一种方法可以循环它意味着当它完成输出它会问你想要制作更多的三角形吗?是还是不是。是的,它会再次执行该程序。不,它会结束它。你是否使用if else语句?

2 个答案:

答案 0 :(得分:0)

在Triangle类和TriangleDriver类中都有一个名为inputTriangleSides()的方法。

在您的主程序中,您可以调用Triangle类中的方法,该方法不会打印出任何内容。

尝试

TriangleDriver yourTriangle= new TriangleDriver();
yourTriangle.inputTriangleSides();

代替。

答案 1 :(得分:0)

由于这看起来像“家庭作业”或测试问题,我将对结构提出一些建议,而不是提供准确的代码。

首先,将程序拆分为Controller类和Triangle类。拥有Controller类,读取输入面并从这些边trialTriangle = new Triangle(side1, side2, side3)创建一个新的Triangle对象。在Triangle类中,创建以下方法:boolean isTriangle(); String getType()int getPerimeter();一些逻辑提示:在对三边形进行排序之后(例如在数组中sides[] = new int[3]确定在查看三角形的类型之前是否有三角形(以确保0,0,0不显示为等边三角形);等边与side [0] == side [2]相同,因为边是有序的;然后确定isocolese(你只需要看看前两个或后两个排序边是否相等)。

在“控制器”类中,获取一行,看看用户是否要求“结束”;如果没有创建三角形并打印结果和循环。