I’m writing for fun a random encounter generator for a D&D campaign using Java!
I ran into a problem, maybe you can help me!
I launch a D20 die, with that result the program process the encounters.
With a result of 1 to 13 the return has to be null because there’s no encounter.
With a result of 14 to 15 the result has to be a terrain encounter.
With a result of 16 to 17 the result has to be a creature encounter.
With a result of 18 to 20 the result has to be a creature and terrain encounter combine.
package underdark;
import creature.CreatureUnderdark;
import terreni.IncontriTerreni;
import utilities.Launchable;
public class IncontriUnderdark implements Launchable{
static String risultato;
public IncontriUnderdark() {
super();
}
/**
* Metodo che determina se il party effettui un incontro nell'underdark
* @return risultato dell'incontro
*/
public static String getIncontriUnderdark() {
int D20= getD20();
switch(D20) {
case 1,2,3,4,5,6,7,8,9,10,11,12,13 : return risultato = null;
case 14,15 : return risultato+= D20 + IncontriTerreni.getTerreno();
case 16,17 : return risultato+= D20 + CreatureUnderdark.getCreatura();
case 18,19,20 : return risultato+= D20 + IncontroMisto();
}
return risultato;
}
/**
* Metodo che determina un incontro misto fra terreni e creature
* @return stringa con il risultato misto
*/
private static String IncontroMisto() {
String risultatoMisto ="";
risultatoMisto +=IncontriTerreni.getTerreno();
risultatoMisto += CreatureUnderdark.getCreatura();
return risultatoMisto;
}
public static int getD20() {
int D20;
return D20 = Launchable.getD20();
}
}
In the app.java file I check the return of the string risultato and control if risultato is null or not then print a line
package App;
import java.util.Scanner;
import underdark.IncontriUnderdark;
public class App {
static Scanner in;
public static void main(String[] args) {
in = new Scanner(System.in);
int scelta = 0;
do {
visualizzaMenu();
scelta = leggiIntero();
switch(scelta) {
case 1:visualizzaIncontroUnderdark() ; break;
case 2: ; break;
case 3: ; break;
case 4: ; break;
case 5: ; break;
case 6: ; break;
case 0: System.out.println("Programma terminato"); break;
default: System.out.println("Scelta non valida");
}
}while(scelta != 0);
in.close();
}
private static void visualizzaIncontroUnderdark() {
String risultatoIncontro = IncontriUnderdark.getIncontriUnderdark();
if(risultatoIncontro != null) {
System.out.println(risultatoIncontro);
System.out.println();
System.out.println();
}else {
System.out.println("Non trovate nulla");
System.out.println();
System.out.println();
}
}
private static void visualizzaMenu() {
System.out.println("Scegli opzione:");
System.out.print("1. Incontri Underdarkn"
+ "2. n"
+ "3.n"
+ "4. n"
+ "5. n"
+ "6. n"
+ "0. uscire dal programman> ");
}
/**
* Metodo che restituisce l'intero letto da tastiera correggendo
* il bug della nextInt
* @return intero letto
*/
private static int leggiIntero() {
int n = in.nextInt();
in.nextLine();
return n;
}
}
The problem is that in the console some of the results are like this :
null19 Cornice rialzata Imboscata
null14 Caverna di funghi
And I don’t understand why, can you help me? 🙂
I’ve tried .Equals() method and to combine the result of the string to a boolean variable to do the check