Zum Inhalt springen

trash.p.

Mitglieder
  • Gesamte Inhalte

    3
  • Benutzer seit

  • Letzter Besuch

  1. Yo das habe ich jetzt soweit, aber es sind immer noch fragen offen. Ich habe die fragen an die spezifischen Stellen in den code geschrieben. Bitte um hilfe, weiß echt nicht mehr weiter :confused: import java.util.Random; import java.util.Scanner; public class dotcom { static int xKoordinate; static int yKoordinate; int anzahltreffer; int zielFelder; // Spielfeldgröße durch Benutzereingabe holen public static void spielFeldGroeße () { //Spielfeld größe über die User-Eingabe mittels der Scanner-Class bestimmen Scanner sc = new Scanner(System.in); // Zeilenangabe System.out.println("Bitte geben Sie die Zeilenanzahl des Spielfeldes an. - Maximale Zeilenanzahl: 10"); xKoordinate = sc.nextInt(); //falls die eingegebenen koordinaten größer 10 sind müssen sie erneut eingegeben werden while (xKoordinate > 10){ System.out.println("OOOOOHPS - Die Eingabe war falsch!"); System.out.println("Bitte geben Sie jeweils eine Zahl zwischen 1 und 10 ein."); System.out.println("Das Spielfeld soll maximal 10x10 Felder enthalten."); System.out.println("Bitte Probieren Sie es gleich nochmal."); System.out.println("Bitte geben Sie die Zeilenanzahl des Spielfeldes an. - Maximale Zeilenanzahl: 10"); xKoordinate = sc.nextInt(); } //Spaltenangabe System.out.println("Bitte geben Sie die Spaltenanzahl des Spielfeldes an. - Maximale Spaltennanzahl: 10"); yKoordinate = sc.nextInt(); // //falls die eingegebenen koordinaten größer 10 sind müssen sie erneut eingegeben werden while (yKoordinate > 10){ System.out.println("OOOOOHPS - Die Eingabe war wiedermal falsch!"); System.out.println ("Bitte geben Sie jeweils eine Zahl zwischen 1 und 10 ein."); System.out.println("Das Spielfeld soll maximal 10x10 Felder enthalten."); System.out.println("Probieren Sie vorher auf die Tasten zu schauen"); System.out.println("Bitte geben Sie die Spaltenanzahl des Spielfeldes an. - Maximale Spaltennanzahl: 10"); yKoordinate = sc.nextInt(); } } //Spielfeldausgabe : Das Design des Spielfeldes ausgeben public static void spielFeldDesign (){ char [][] fieldUser = new char [xKoordinate][yKoordinate]; System.out.print(" "); for (xKoordinate = 0; xKoordinate< fieldUser.length; xKoordinate++ ){ System.out.printf(" %2d ", xKoordinate+1); } System.out.println(); for (int yKoordinate = 0; yKoordinate < fieldUser[0].length; yKoordinate++){ System.out.print(" +"); for(int xKoordinate = 0; xKoordinate < fieldUser.length; xKoordinate++){ System.out.print("---+"); } System.out.println(); System.out.printf("%2d|", yKoordinate+1); for(int xKoordinate = 0; xKoordinate < fieldUser.length; xKoordinate++) { fieldUser [xKoordinate][yKoordinate] = '@'; System.out.print(" " + fieldUser[xKoordinate][yKoordinate]+ " |"); }// System.out.println(); }// }// // DotComs per Zufall auf das Spielfeld setzen // setzeDotCom /* Wie bekomme ich es nun hin die DotComs per Zufall aufs Spielfeld zu setzen ohne das diese über die Ränder des Arrays schießen? */ // Zufallszahlen deklarieren public static void setzeDotComs(){ //Dotcoms in Array String [] dotComs = new String[6]; dotComs[0]="moodle"; dotComs[1]="amazon"; dotComs[2]="uni-due"; dotComs[3]="google"; dotComs[4]="youtube"; dotComs[5]="studivz"; dotComs[6]="facebook"; char[] word = dotComs[0].toCharArray(); // Den String in ein Char umgewandelt um ihn in der random-methode zu nutzen } // Zufallsbelegung auf dem feld // Wie übergebe ich nun der random-methode den Char Array? private static int randomInt( ? ) { // was muss hier deklariert damit die Random - Methode auf den Char zugreifen kann??? Random rand = new Random(); /* Vorschlag if(Keine Ahnung)) { int breite; int hoehe; breite= randomInt(xKoordinate-word.length); hoehe = randomInt(yKoordinate); } else { int breite; int hoehe; breite= randomInt(xKoordinate); hoehe = randomInt(yKoordinate-word.length); */ } /* Wie soll das nur gehen die Zufallszahlen mit den Dotcoms und dem Spielfeld so zu verknüpfen, dass diese zufällige und in ihrer Wortlänge gesetzt werden? */ public static void userSchuss(){ int x = 0; int y = 0; Scanner sc = new Scanner(System.in); System.out.println ("Bitte geben Sie eine Spaltenkoordinate ein!"); x = Integer.parseInt(sc.next()); System.out.println ("Bitte geben Sie eine Zeilenkoordinate ein!"); y = Integer.parseInt(sc.next()); } public static void main ( String [] args){ spielFeldGroeße(); spielFeldDesign(); setzeDotComs(); userSchuss(); } } please help...
  2. danke das Problem habe ich aus der welt geschafft. NullPointerException heißt ja soviel das die Anweisung ins nichts, bzw. leere führt deswegen war das ja klar.. danke nochmal! Nun habe ich das Array initialiert. und bekomme nun eine andere Fehlermeldung Hier erstmal mein code-update import java.util.Random; import java.util.Scanner; public class dotcom { static int xKoordinate; static int yKoordinate; static String[] DotComPositionen; int anzahltreffer; int zielFelder; public static void spielFeldGroeße () { //Spielfeld größe über die User-Eingabe mittels der Scanner-Class bestimmen Scanner sc = new Scanner(System.in); // Zeilenangabe System.out.println("Bitte geben Sie die Zeilenanzahl des Spielfeldes an. - Maximale Zeilenanzahl: 10"); xKoordinate = sc.nextInt(); //falls die eingegebenen koordinaten größer 10 sind müssen sie erneut eingegeben werden while (xKoordinate > 10){ System.out.println("OOOOOHPS - Die Eingabe war falsch!"); System.out.println("Bitte geben Sie jeweils eine Zahl zwischen 1 und 10 ein."); System.out.println("Das Spielfeld soll maximal 10x10 Felder enthalten."); System.out.println("Bitte Probieren Sie es gleich nochmal."); System.out.println("Bitte geben Sie die Zeilenanzahl des Spielfeldes an. - Maximale Zeilenanzahl: 10"); xKoordinate = sc.nextInt(); } //Spaltenangabe System.out.println("Bitte geben Sie die Spaltenanzahl des Spielfeldes an. - Maximale Spaltennanzahl: 10"); yKoordinate = sc.nextInt(); // //falls die eingegebenen koordinaten größer 10 sind müssen sie erneut eingegeben werden while (yKoordinate > 10){ System.out.println("OOOOOHPS - Die Eingabe war wiedermal falsch!"); System.out.println ("Bitte geben Sie jeweils eine Zahl zwischen 1 und 10 ein."); System.out.println("Das Spielfeld soll maximal 10x10 Felder enthalten."); System.out.println("Probieren Sie vorher auf die Tasten zu schauen"); System.out.println("Bitte geben Sie die Spaltenanzahl des Spielfeldes an. - Maximale Spaltennanzahl: 10"); yKoordinate = sc.nextInt(); } } //Spielfeldausgabe : Das Design des Spielfeldes ausgeben public static void spielFeldDesign (){ char [][] fieldUser = new char [xKoordinate][yKoordinate]; System.out.print(" "); for (xKoordinate = 0; xKoordinate< fieldUser.length; xKoordinate++ ){ System.out.printf(" %2d ", xKoordinate+1); } System.out.println(); for (int yKoordinate = 0; yKoordinate < fieldUser[0].length; yKoordinate++){ System.out.print(" +"); for(int xKoordinate = 0; xKoordinate < fieldUser.length; xKoordinate++){ System.out.print("---+"); } System.out.println(); System.out.printf("%2d|", yKoordinate+1); for(int xKoordinate = 0; xKoordinate < fieldUser.length; xKoordinate++) { fieldUser [xKoordinate][yKoordinate] = '@'; System.out.print(" " + fieldUser[xKoordinate][yKoordinate]+ " |"); }// System.out.println(); }// }// // DotComs per Zufall auf das Spielfeld setzen public static void setzeDotComs() { String[] DotComPositionen = new String[7]; // Initialiesierung des DotComPositionieren-Arrays DotComPositionen[1] = "moodle"; DotComPositionen[2] = "amazon"; DotComPositionen[3] = "uni-due"; DotComPositionen[4] = "google"; DotComPositionen[5] = "youtube"; DotComPositionen[6] = "studivz"; DotComPositionen[7] = "facebook"; } // setzeDotCom // Wie bekomme ich es nun hin die DotComs per Zufall aufs Spielfeld zu setzen ohne das diese über die Ränder des Arrays schießen // Zufallszahlen deklarieren private int zufallX(int max){ Random rand = new Random(); return rand.nextInt(10); } private int zufallY (int max){ Random rand = new Random(); return rand.nextInt(10); } public static void main ( String [] args){ spielFeldGroeße(); spielFeldDesign(); setzeDotComs(); } } die Fehlermeldung bei der Ausgabe: Bitte geben Sie die Zeilenanzahl des Spielfeldes an. - Maximale Zeilenanzahl: 10 10 Bitte geben Sie die Spaltenanzahl des Spielfeldes an. - Maximale Spaltennanzahl:10 10 1 2 3 4 5 6 7 8 9 10 +---+---+---+---+---+---+---+---+---+---+ 1| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | +---+---+---+---+---+---+---+---+---+---+ 2| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | +---+---+---+---+---+---+---+---+---+---+ 3| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | +---+---+---+---+---+---+---+---+---+---+ 4| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | +---+---+---+---+---+---+---+---+---+---+ 5| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | +---+---+---+---+---+---+---+---+---+---+ 6| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | +---+---+---+---+---+---+---+---+---+---+ 7| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | +---+---+---+---+---+---+---+---+---+---+ 8| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | +---+---+---+---+---+---+---+---+---+---+ 9| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | +---+---+---+---+---+---+---+---+---+---+ 10| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at dotcom.setzeDotComs(dotcom.java:110) at dotcom.main(dotcom.java:134) Heißt java.lang.ArrayIndexOutOfBoundsException: 7 nicht soviel, das die dotComs über die ränder des Spielfeldes hinaur ragen und deswegen nicht angezeigt werden können? Wie kann man den dieses Problem nur umgehen. Hilfe....
  3. Hallo, ich muss für die Uni ein "DotComVersenken-Spiel" aufbauen. Die Spielfeldgröße wird über die Benutzereingabe bestimmt, darf maximal 10 x 10 Felder beinhalten. Es gilt folgende DotComs zu versenken: "moodle"; "amazon"; "uni-due"; "google"; "youtube"; "studivz"; "facebook"; Diese sollen nun per Zufall auf das Spielfeld verteilt werden. Um die zufahlszahl zu generieren soll die Random() Methode benutzt werden. Danach soll er Spiel nach den kKoordinaten gefragt werden die er abschießen will. Wenn ein dotsom getroffen worden ist soll Treffer angezeit werden, wenn nicht vorbei. Mein problem liegt aber schon darin die DotComs per zufall auf das Spielfeld setzen zu lassen, ich bekomme andauern Fehlermelungen und brauch unbedingt hilfe da ich noch ein ziemliecher newbie in JAVA bin fehlen mir auch die richtigen Ansätze für das Problem Mein java code import java.util.Random; import java.util.Scanner; public class dotcom { static int xKoordinate; static int yKoordinate; static String[] DotComPositionen; int anzahltreffer; int zielFelder; public static void spielFeldGroeße () { Scanner sc = new Scanner(System.in); System.out.println("Bitte geben Sie die Zeilenanzahl des Spielfeldes an. - Maximale Zeilenanzahl: 10"); xKoordinate = sc.nextInt(); while (xKoordinate > 10){ System.out.println("OOOOOHPS - Die Eingabe war falsch!"); System.out.println("Bitte geben Sie jeweils eine Zahl zwischen 1 und 10 ein."); System.out.println("Das Spielfeld soll maximal 10x10 Felder enthalten."); System.out.println("Bitte Probieren Sie es gleich nochmal."); System.out.println("Bitte geben Sie die Zeilenanzahl des Spielfeldes an. - Maximale Zeilenanzahl: 10"); xKoordinate = sc.nextInt(); } System.out.println("Bitte geben Sie die Spaltenanzahl des Spielfeldes an. - Maximale Spaltennanzahl: 10"); yKoordinate = sc.nextInt(); // while (yKoordinate > 10){ System.out.println("OOOOOHPS - Die Eingabe war wiedermal falsch!"); System.out.println ("Bitte geben Sie jeweils eine Zahl zwischen 1 und 10 ein."); System.out.println("Das Spielfeld soll maximal 10x10 Felder enthalten."); System.out.println("Probieren Sie vorher auf die Tasten zu schauen"); System.out.println("Bitte geben Sie die Spaltenanzahl des Spielfeldes an. - Maximale Spaltennanzahl: 10"); yKoordinate = sc.nextInt(); } } public static void spielFeldDesign (){ char [][] fieldUser = new char [xKoordinate][yKoordinate]; System.out.print(" "); for (xKoordinate = 0; xKoordinate< fieldUser.length; xKoordinate++ ){ System.out.printf(" %2d ", xKoordinate+1); } System.out.println(); for (int yKoordinate = 0; yKoordinate < fieldUser[0].length; yKoordinate++){ System.out.print(" +"); for(int xKoordinate = 0; xKoordinate < fieldUser.length; xKoordinate++){ System.out.print("---+"); } System.out.println(); System.out.printf("%2d|", yKoordinate+1); for(int xKoordinate = 0; xKoordinate < fieldUser.length; xKoordinate++) { fieldUser [xKoordinate][yKoordinate] = '@'; System.out.print(" " + fieldUser[xKoordinate][yKoordinate]+ " |"); }// System.out.println(); }// }// private int zufallX(int max){ Random rand = new Random(); return rand.nextInt(10); } private int zufallY (int max){ Random rand = new Random(); return rand.nextInt(10); } public static void setzeDotComs() { DotComPositionen[1] = "moodle"; DotComPositionen[2] = "amazon"; DotComPositionen[3] = "uni-due"; DotComPositionen[4] = "google"; DotComPositionen[5] = "youtube"; DotComPositionen[6] = "studivz"; DotComPositionen[7] = "facebook"; } public static void main ( String [] args){ spielFeldGroeße(); spielFeldDesign(); setzeDotComs(); } } Und die Fehlerausgabe der Ausgabe auf der Kommandozeile: Bitte geben Sie die Zeilenanzahl des Spielfeldes an. - Maximale Zeilenanzahl: 10 10 Bitte geben Sie die Spaltenanzahl des Spielfeldes an. - Maximale Spaltennanzahl: 10 10 1 2 3 4 5 6 7 8 9 10 +---+---+---+---+---+---+---+---+---+---+ 1| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | +---+---+---+---+---+---+---+---+---+---+ 2| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | +---+---+---+---+---+---+---+---+---+---+ 3| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | +---+---+---+---+---+---+---+---+---+---+ 4| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | +---+---+---+---+---+---+---+---+---+---+ 5| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | +---+---+---+---+---+---+---+---+---+---+ 6| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | +---+---+---+---+---+---+---+---+---+---+ 7| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | +---+---+---+---+---+---+---+---+---+---+ 8| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | +---+---+---+---+---+---+---+---+---+---+ 9| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | +---+---+---+---+---+---+---+---+---+---+ 10| @ | @ | @ | @ | @ | @ | @ | @ | @ | @ | Exception in thread "main" java.lang.NullPointerException at dotcom.setzeDotComs(dotcom.java:101) at dotcom.main(dotcom.java:118) Ich hoffe es kann mir jemand helfen... schönen gruß

Fachinformatiker.de, 2024 by SE Internet Services

fidelogo_small.png

Schicke uns eine Nachricht!

Fachinformatiker.de ist die größte IT-Community
rund um Ausbildung, Job, Weiterbildung für IT-Fachkräfte.

Fachinformatiker.de App

Download on the App Store
Get it on Google Play

Kontakt

Hier werben?
Oder sende eine E-Mail an

Social media u. feeds

Jobboard für Fachinformatiker und IT-Fachkräfte

×
×
  • Neu erstellen...