2014-02-18 68 views
-1
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(); 
} 
} 

即時得到的code.Method displaySortedNumber類三角displayedSortedNumber部分的差錯不能被應用到給定的類型;必需int,int,int;發現沒有爭論的原因:實際和正式的爭論名單長度不同。我將如何解決這個問題?我試着在輸入三個數字的地方得到一個輸出,它會對數字進行排序,看他們是否做出了三角形,它是什麼類型,並獲得了周長。

+0

1)現在,將displaySortedNumbers();方法需要3個參數。 (你沒有任何調用它)2)也許這三個參數應該是你的Triangle類中的屬性,並從方法簽名中移除。 – wxyz

回答

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

它說它找不到符號 - 變量YourTriangle – user3296193

+0

檢查拼寫。 也許你輸入了「Y」我們的三角形而不是「y」我們的三角形 –