im starting my adventure with learning to code in Java. Im stuck on an example from book. It does compile but there is no effect when I run it. The output is just blank. Im using Visual studio code. The example says that all 3 separate files (ProstyPortal, ProstyPortalGra & PomocnikGry) should be in 1 folder. When i try to start a file with main also nothing happens. I decided to join them all into one file but still i do get no result. The code is basically one line battleship game example.
ftp://ftp.helion.pl/przyklady/javrg2.zip - in folder r05 there are files (ProstyPortal, ProstyPortalGra & PomocnikGry) from the example that should work when in the same folder but they dont. Damn, its really hard to find whats wrong when you just start learning :P
Its an example from chapter nr 5.
I did follow all the rules and sugestions as in the book but even a straight copying the code does not help. the previous examples i did run without bigger issues.
import java.io.*;
class ProstyPortalGra {
public static void main(String[] args) {
int iloscRuchow = 0;
PomocnikGry pomocnik = new PomocnikGry();
ProstyPortal portal = new ProstyPortal();
int liczbaLosowa = (int) (Math.random() * 5);
int[] polozenie = {liczbaLosowa, liczbaLosowa+1, liczbaLosowa+2};
portal.setPolaPolozenia(polozenie);
boolean czyIstnieje = true;
while (czyIstnieje == true) {
String pole = pomocnik.pobierzDaneWejsciowe("Podaj liczbę");
String wynik = portal.sprawdz(pole);
iloscRuchow++;
if (wynik.equals("zatopiony")) {
czyIstnieje = false;
System.out.println(iloscRuchow + " ruchów");
} // koniec if
} // koniec while
} // koniec main
}
class ProstyPortal {
int [] polaPolozenia;
int iloscTrafien;
public void setPolaPolozenia(int[] ppol) {
polaPolozenia = ppol;
}
public String sprawdz(String stringPole) {
int strzal = Integer.parseInt(stringPole);
String wynik = "pudło";
for (int pole : polaPolozenia) {
if (strzal == pole) {
wynik = "trafiony";
iloscTrafien++;
break;
}
} // koniec pętli
if (iloscTrafien == polaPolozenia.length) {
wynik = "zatopiony";
}
System.out.println(wynik);
return wynik;
} // koniec metody
} // koniec klasy
class PomocnikGry {
public String pobierzDaneWejsciowe(String komunikat) {
String wierszWej = null;
System.out.print(komunikat + " ");
try {
BufferedReader sw = new BufferedReader(
new InputStreamReader(System.in));
wierszWej = sw.readLine();
if (wierszWej.length() == 0) return null;
} catch (IOException e) {
System.out.println("IOException: " + e);
}
return wierszWej;
}
}