Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Hallo,

statt mit C/C++ soll ich jetzt das Spiel "Schiffe versenken" mit Java programmieren. Allerdings kann bei mir in der Firma so gut wie keiner (nur wenige Ingenieure aus Studienzeiten) das.

Bei der zu sehenden Ausgabe will ich das Spielfeld als char-Array ausgeben.

Allerdings erscheinen bei allen möglichen Varianten nur eckige schmale Nuller auf dem Bildschirm.

Mein Code:

package Schiffevers;


public class spielfeld {



		public static int [][] zahlenarray = new int [10] [10];

		public static char [][] chararray = new char [10][10];


		spielfeld()

		{

			for (int y_achse = 0; y_achse < 10; y_achse++)

			{

				for (int x_achse = 0; x_achse < 10; x_achse++)

				{

					zahlenarray [y_achse][x_achse] = 0;

					chararray [y_achse][x_achse] = 'X';

				}

			}

		}


		public static int get_spielfeld(int x_achse, int y_achse)

		{

			return zahlenarray [y_achse][x_achse];

		}


		[B]public static int get_char_spielfeld(int x_achse, int y_achse)

		{

			return chararray [y_achse][x_achse];

		}[/B]


		public static void set_spielfeld (int x, int y, int wert)

		{

			zahlenarray [y][x]= wert;

		}




		public static void print_spielfeld ()

		{


			for (int y_achse = 0; y_achse < 10; y_achse++)

			{

				for (int x_achse = 0; x_achse < 10; x_achse++)

				{

					System.out.printf("%d ", zahlenarray [y_achse][x_achse]);	

					if (x_achse == 9)

					{

						System.out.println();

					}

				}

			}	

		}


		[B]public static void print_char_spielfeld ()

		{


			for (int y_achse = 0; y_achse < 10; y_achse++)

			{

				for (int x_achse = 0; x_achse < 10; x_achse++)

				{

					System.out.print(" " + chararray [y_achse][x_achse] + " ");	

					if (x_achse == 9)

					{

						System.out.println();

					}

				}

			}	

		}

		[/B]

		[B]public static void set_char_spielfeld(int x, int y, char wert){

			chararray [y][x] = wert;

		}[/B]





}

Ich habe es auch schon in der gewohnten Form mit

- system.out.printf (" %c", chararray [y_achse][x_achse]);

- system.out.printf ( '%c' , chararray [y_achse][x_achse]); (C/C++)

ausprobiert. Es kommt immer das Selbe raus.

Im Internet habe ich gelesen, dass die Java Virtual Machine chars als Ints sieht.

Was kann ich machen um das char-Array auszugeben.

X = noch nicht geschossen

T = Treffer

0 = nicht getroffen

V = versenkt

P.S. Ich bin Systemintegratorin im 2. Lehrjahr. In der Schule steht im 2. und 3. Lehrjahr Java auf dem Plan, aber wir haben damit noch nicht angefangen. Zuerst sollen C und C++ sitzen. Ich soll quasi vorarbeiten.

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.