2014-12-19 100 views
-4

我有一個程序來尋找畢達哥拉斯三元組。在它裏面,我有一個需要用來調用方法的對象。所述物體被破壞。錯誤是「方法Triples(int)未定義類型Triples」和「方法greatesCommonFactor()對於Triples類型未定義」請注意,並非Triples中的所有內容都會有用的東西atm。它尚未完全完成。類型方法未定義方法?

public class TriplesRunner 
{ 
    public static void main(String args[]) 
    { 
     int number; 
     Scanner keyboard = new Scanner(System.in); 
     System.out.print("Enter the natural number :: "); 
     number=keyboard.nextInt(); 

     Triples test = new Triples(); 
     test.Triples(number); 
     test.greatestCommonFactor(number); 
     System.out.println(test.toString()); 
    } 
} 



public class Triples 
{ 
    public int number; 


    public Triples(int num) 
    { 
     setNum(number); 
    } 

    public void setNum(int num) 
    { 
     int a = 0; 
     int b = 0; 
     int c = 0; 
    } 

    public int greatestCommonFactor(int a, int b, int c) 
    { 
     int max = 0; 
     for(a=1; a<=number-2; a++) 
     { 
      for(b=a+1; b<=number-1; b++) 
      { 
       for(c=b+1; c<=number; c++) 
       { 
        if(a*a + b*b == c*c); 
        } 
       } 
      } 
     return 1; 
    } 

    public String toString() 
    { 
     String output=""; 
     output+="a + b + c"; 
     return output+"\n"; 
    } 
} 
+1

你沒有方法在名爲'Triples'你'三倍'類。 –

+3

您將構造函數與方法混淆。他們不一樣。 –

+1

您的半粘貼錯誤有一個拼寫錯誤:_greates_。代碼似乎沒有這個錯誤,但這就是爲什麼你不會解釋錯誤。 – keyser

回答

2

您嘗試調用構造函數的方法,

更改這一部分:

Triples test = new Triples(); 
test.Triples(number); 

Triples.test = new Triples(number); 
2

Triples不是一個方法 - 這是你的構造,這意味着它與new運營商調用:

Triples test = new Triples(number); 

greatestCommonFactor定義不正確。目前,它有三個int參數,而不是採取沒有使用Triples'數據成員:

public int greatestCommonFactor() 
+0

你剛剛結束時輸入了三元組,並在此之前輸入了「greatCommonFactor」 –

+0

@newbiedoodle - true和true,兩者都固定。謝謝! – Mureinik