2016-11-29 76 views
-2

這就是我目前爲止的內容。它編譯和運行,但直到用戶放在每個長度的一側。信息輸入後,程序應告訴用戶它是什麼三角形,但不是。我正在編寫一個代碼,通過獲取用戶各邊的長度來確定三角形的類型(等邊,斜角,等腰三角形)。

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

你需要調用函數'isEqualateral','isIsoceles'和'isScalene'。 –

回答

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