我正在编写一个代码,通过从用户获取每一边的长度来确定三角形的类型(等边,斜角,等腰)

时间:2016-11-29 16:18:48

标签: java

这是我到目前为止所拥有的。它编译并运行,但直到用户放入每个长度的一侧。在输入信息之后,程序应该告诉用户它是什么三角形,但不是。

import java.util.Scanner; //declare scanner

public class SideClassification {

public static void main(String [] args) 
{

Scanner input = new Scanner (System.in); //declare scanner

//declare sides of triangle
int sideA = 0;
int sideB = 0;
int sideC = 0;

System.out.print ("Enter the length of side A: "); //ask user for length of side A
sideA = input.nextInt();

System.out.print ("Enter the length of side B: "); //ask user for length of side B
sideB = input.nextInt();

System.out.print ("Enter the length of side C: "); //ask user for lenght of side C
sideC = input.nextInt();  

}

public static boolean isEqualateral (int sideA, int sideB, int sideC) //if statement for equalateral triangle
{
if ((sideA == sideB) && (sideB == sideC))
{
  return true;
}
else
{
  return false;
}
}

public static boolean isIsoceles (int sideA, int sideB, int sideC) //if statement for isoceles triangle
{
if((sideA == sideB) || (sideA == sideC) || (sideB == sideC)) 
{
  return true;
}
else
{
  return false;
}
}

public static boolean isScalene (int sideA, int sideB, int sideC) //if statement for scalene triangle
{
if((sideA != sideB) && (sideA != sideC) && (sideB != sideC))
{
  return true;
}
else
{
  return false;
}
  }
}

3 个答案:

答案 0 :(得分:1)

你永远不会实际调用这些函数:

public static void main(String [] args) 
{
    Scanner input = new Scanner (System.in); //declare scanner
    // Omitted for brevity

    // Call the functions and print results
    if (isEqualateral(sideA, sideB, sideC)) System.out.println("isEqualateral");
    if (isIsoceles(sideA, sideB, sideC)) System.out.println("isIsoceles");
    if (isScalene(sideA, sideB, sideC)) System.out.println("isScalene");
}

答案 1 :(得分:0)

您实际上从未在主方法中打印答案。你做的最后一件事是得到输入,所以当然它不会输出任何东西。

答案 2 :(得分:0)

您忘记实际调用静态方法isEqualateral(以及其他类型的方法)来确定类型:

在获取用户输入后的主要方法中,您应该这样做:

if(isEqualateral(sideA,sideB,sideC)) {
    System.out.println("Triangle is equalateral.");
}else if(isScalene(sideA,sideB,sideC)){
    System.out.println("Triangle is scalene.");
}else if(isIsoceles(sideA,sideB,sideC)) {
    System.out.println("Triangle is isoceles.");
}else{
    System.out.println("Unknown type! Check your input");
}