2016-03-03 76 views
1
import java.io.*; 

class Main0 
{ 
    public static void main(String args[]) 
    { 
     String onoma, epitheto, key01; 
     short AriMit, EtosEis, AM, key02; 
     int i, pos; 
     Foititis pinakas[] = new Foititis(3); 
     for (i = 0; i < pinakas.length; i++) 
     {  
      System.out.println("Ola ta stoixeia na einai taksinomimena symfwna me ton Arithmo Mitroou." + "\n" + "Dwste Onoma."); 
      do 
      { 
       onoma = FUserInput.getString(); 
       if (onoma == "x") 
        System.out.println("Mh egkyrh timh. Epanalagete"); 
      } while (onoma == "x"); 

      System.out.println("Dwste Epitheto."); 
      do 
      { 
       epitheto = FUserInput.getString(); 
       if (epitheto == "x") 
        System.out.println("Mh egkyrh timh. Epanalagete"); 
      } while (epitheto == "x"); 

      System.out.println("Dwste Arithmo Mitrwou."); 
      do 
      { 
       AriMit = FUserInput.getshort(); 
       if (AriMit == -1) 
        System.out.println("Mh egkyrh timh. Epanalagete"); 
      } while (AriMit == -1); 

      System.out.println("Dwste Etos Eisagwghs."); 
      do 
      { 
       EtosEis = FUserInput.getshort(); 
       if (EtosEis == -1) 
        System.out.println("Mh egkyrh timh. Epanalagete"); 
      } while (EtosEis == -1); 

      pinakas[i] = new Foititis(onoma, epitheto, AriMit, EtosEis); 
     } 
     pos = 1; 
     System.out.println(Foititis.toString(pos, pinakas)); 
    } 
} 

class Foititis  
{ 
    private String onoma, epitheto; 
    private short AriMit, EtosEis; 

    public Foititis (String on, String ep, short AM, short EE) 
    { 
     onoma = on; 
     epitheto = ep; 
     AriMit = AM; 
     EtosEis = EE; 
    } 

    public String getEpwnymo()  //alles klaseis 
    { 
     return epitheto; 
    } 

    public String toString(int j, Foititis b[]) 
    { 
     String emf; 
     emf = b[j].onoma; 
    } 
} 

class FUserInput   
/*dedomenwn kateli3a na xrisimopoihsw thn "FUserInput" gia ola ta dedomena, anti na kanw "catch" to "exception" gia kathe scanner ksexwrista, kathws eixa skopo na xrisimopoihsw mono scanner)*/ 
{ 
    static String getString() 
    { 
     String line; 
     InputStreamReader eisodosDouble = new InputStreamReader(System.in); 
     BufferedReader br = new BufferedReader(eisodosDouble); 
     try 
     { 
      line = br.readLine(); 
      return line; 
     } 
     catch(Exception e) 
     { 
      return "x"; 
     } 
    } 

    static short getshort() 
    { 
     String line; 
     InputStreamReader eisodosDouble = new InputStreamReader(System.in); 
     BufferedReader br = new BufferedReader(eisodosDouble); 
     try 
     { 
      line = br.readLine(); 
      short i = Short.parseShort(line); 
      return i; 
     } 
     catch(Exception e) 
     { 
      return -1; 
     } 
    } 
} 

這不是任何東西的重複,我不是問我的字符串方程式,而是關於我想要使用的toString方法!在對象數組上使用toString

所以我有這個代碼...它所要做的就是創建一個類型爲Foititis的對象數組,名爲pinakas並具有四個屬性。如果你看類Foititis,你會注意到一個toString方法,我想要做的就是使用toString方法在屏幕上顯示一些東西(在這個例子中它簡化爲數組對象的onoma屬性)。請注意,我也想發送我想要看到的數組的某個位置(pos - > j),在這個例子中,位置是固定的(設置爲1),但是在實際的代碼中它不會。

我試着編譯它,我得到了這兩個錯誤,第二個可能是合理的,因爲我沒有以正確的方式編寫代碼,但第一個呢?我又做了同樣的事情,我沒有任何問題(創建一個對象數組)。

The error

+0

也http://stackoverflow.com/questions/4969171/cannot-make-static-reference-to -non-static-method – fabian

+0

@MarounMaroun即使他的if和while中的'==「x」'應該替換爲「.equals(」x「)」,這並不是他在他的問題中所要求的。在他連接底部的錯誤中,他的數組存在問題(請參閱下面的@mikea答案),並且他在靜態調用非靜態方法時遇到問題(System.out.println(Foititis .toString(pos,pinakas));'應該用'System.out.println(pinakas [pos] .toString());'和'Foititis'中的'toString'方法替換爲'public String toString (){return onoma;}' –

+0

@KevinCruijssen非常感謝!IT WORKED!和 –

回答

1

您試圖聲明數組,所以它應該是:

Foititis pinakas[] = new Foititis[3]; 
+0

真的嗎?我怎麼錯過了...無論如何謝謝你 –

相關問題