Ich habe da mal ein Problem. Und zwar geht es darum, dass ich ein Spiel programmieren soll und da nun schon am Anfang hänge. Mein Problem ist folgendes: Ich möchte ein 2d-Array erstellen und es mit Werten befüllen. Das hab ich auch hinbekommen. Nun will ich einen Wert aus dem Array, das ich gerade befüllt habe wieder ausgeben lassen. Und da ist dann mein Problem. Wie mach ich das?
Hier mein Quelltext:
Code:
public class LineThem{
public String[][] tabelle;{
String[][] tabelle = new String[3][3];
tabelle[0][0]="x";
tabelle[0][1]="x";
tabelle[0][2]="o";
tabelle[1][0]="o";
tabelle[1][1]="x";
tabelle[1][2]="o";
tabelle[2][0]="x";
tabelle[2][1]="o";
tabelle[2][2]="x";
}
public static void main(String[] args){
System.out.println(tabelle[0][0]);
}
}
Ich bekomme nun jedesmal diese Fehlermeldung:
Cannot make a static reference to the non-static field tabelle
wie willst du es denn haben, statisches Array oder Array eines Objektes?
Code:
public class LineThem{
public String[][] tabelle;
{
// anonymer Konstruktor
tabelle = new String[3][3];
tabelle[0][0]="x";
tabelle[0][1]="x";
tabelle[0][2]="o";
tabelle[1][0]="o";
tabelle[1][1]="x";
tabelle[1][2]="o";
tabelle[2][0]="x";
tabelle[2][1]="o";
tabelle[2][2]="x";
}
public static void main(String[] args){
System.out.println(new LineTem().tabelle[0][0]);
}
}
Code:
public class LineThem{
public static String[][] tabelle;
static {
// anonymer statischer Konstruktor
tabelle = new String[3][3];
tabelle[0][0]="x";
tabelle[0][1]="x";
tabelle[0][2]="o";
tabelle[1][0]="o";
tabelle[1][1]="x";
tabelle[1][2]="o";
tabelle[2][0]="x";
tabelle[2][1]="o";
tabelle[2][2]="x";
}
public static void main(String[] args){
System.out.println(tabelle[0][0]);
}
}
Code:
public class LineThem{
public static String[][] tabelle = {{"x","o",o},{"x","o",o},{"x","o",o}};
public static void main(String[] args){
System.out.println(tabelle[0][0]);
}
}
Also Objekte will ich nicht rein schreiben, also brauch ich wohl nur ein statisches Array. Könnte ich die Ausgabe jetzt auch in eine extra Methode schreiben, die ich dann in der main-methode für meine Tabelle ausführe?
Also in etwa so:
Code:
public void ausgeben(){
System.out.println(tabelle[0][0]);
}
public static void main(String[] args){
tabelle.ausgeben();
}