2013-03-16 61 views
3

我有兩個類Hangmanwords,內容如下:爲什麼我的數組不能打印?

劊子手類:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package hangman; 

import java.util.Random; 
import java.util.Scanner; 

/** 
* 
* @author Adam2_000 
*/ 
public class Hangman { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 

     String player = ""; 
     String selection; 
     Scanner scan = new Scanner(System.in); 

     words words = new words(); 
     String easyWords1[] = words.easyWords; 
     String mediumWords1[] = words.mediumWords; 
     String hardWords1[] = words.hardWords; 


     System.out.println("Welcome to Hangman version 1"); 
     System.out.println("Please choose a difficulty"); 
     System.out.println("A: Easy"); 
     System.out.println("B: Medium"); 
     System.out.println("C: Hard"); 


     char iChoice; 

     do { 
      selection = scan.nextLine().toUpperCase(); 
     } while (selection.isEmpty()); 
     iChoice = selection.charAt(0); 
     if (iChoice != 'X') { 
      switch (iChoice) { 

       case 'A': 
        System.out.println("You have choosen easy:"); 
        System.out.println(words.getEasyWords()); 
        break; 

       case 'B': 
        System.out.println("You have choosen Medium"); 
        System.out.println(words.getMediumWords()); 
        break; 

       case 'C': 
        System.out.println("You have choosen Hard"); 
        System.out.println(words.getHardWords()); 
        break; 
      } 
     } 
    } 
} 

字類:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package hangman; 

import java.lang.reflect.Array; 
import java.util.Random; 


/** 
* 
* @author Adam2_000 
*/ 
public class words extends Hangman { 

     String[] easyWords = {"Bee", "Car", "Fish", "Shed"}; 
     String[] mediumWords = {"House", "Sheep", "Castle", "Phone"}; 
     String[] hardWords = {"Octagon", "Crocodile", "Chocolate", "Motorbike"}; 

    public String[] getEasyWords() { 
     return easyWords; 
    } 

    public void setEasyWords(String[] easyWords) { 
     this.easyWords = easyWords; 
    } 

    public String[] getMediumWords() { 
     return mediumWords; 
    } 

    public void setMediumWords(String[] mediumWords) { 
     this.mediumWords = mediumWords; 
    } 

    public String[] getHardWords() { 
     return hardWords; 
    } 

    public void setHardWords(String[] hardWords) { 
     this.hardWords = hardWords; 
    } 

    @Override 
    public String toString() { 
     return "words{" + "easyWords=" + easyWords + ", mediumWords=" + mediumWords + ", hardWords=" + hardWords + '}'; 
    } 
} 

當我嘗試顯示每個陣列我收到引用而不是字符串可以有人幫助我嗎?

回答

4

...我收到的引用,而不是字符串...

這只是如何使用該方法的類Object定義。

因爲您不能覆蓋陣列的這種行爲,您可以遍歷它,也可以使用的toStringstatic)實用程序方法。

2

你看到像

[Ljava.lang.String;@17bd6a1 

這是一個String陣列的Object#toString respresentation,東西要顯示的內容,你可以使用:

Arrays.toString(words.getEasyWords()) 
+0

真棒非常感謝你! – user2177781 2013-03-16 19:18:37