Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package ch.bbbaden.rio;
  7.  
  8. import javax.swing.JOptionPane;
  9.  
  10. /**
  11.  *
  12.  * @author Manuel Saugy
  13.  */
  14. public class Starter {
  15.  
  16.     /**
  17.      * @param args the command line arguments
  18.      */
  19.     public static void main(String[] args) {
  20.         // TODO code application logic here
  21.         Gui gui = new Gui();
  22.         RealerWuerfel realerwuerfel = new RealerWuerfel();
  23.         Rio rio = new Rio(realerwuerfel);
  24.  
  25.         int anzahlspieler = eingabeZahl("Wie viele spielen mit? (höchstens 6)");
  26.         Spieler[] spieler = new Spieler[anzahlspieler];
  27.  
  28.         for (int i = 0; i < anzahlspieler; i++) {
  29.             String name = eingabeText("Namen");
  30.             int alter = eingabeZahl("Alter");
  31.             int anzahlStaebchen = 10;
  32.             spieler[i] = new Spieler(name, alter, anzahlStaebchen);
  33.         }
  34.  
  35.         Spiel spiel = new Spiel(spieler, gui, rio);
  36.         spiel.spiele();
  37.     }
  38.  
  39.     private static int eingabeZahl(String frage) {
  40.  
  41.         String zahlS = "";
  42.         int zahlI = 0;
  43.         boolean ersteDurchfuehrung = true;
  44.         boolean umwandlungGeklappt;
  45.  
  46.         do {
  47.  
  48.             String prompt = frage;
  49.             if (!ersteDurchfuehrung) {
  50.                 prompt = "Ungültige Eingabe\n" + frage;
  51.             }
  52.  
  53.             zahlS = JOptionPane.showInputDialog(null, prompt, frage,
  54.                     JOptionPane.PLAIN_MESSAGE);
  55.  
  56.             ersteDurchfuehrung = false;
  57.  
  58.             umwandlungGeklappt = true;
  59.  
  60.             try {
  61.                 zahlI = Integer.parseInt(zahlS);
  62.             } catch (NumberFormatException e) {
  63.                 umwandlungGeklappt = false;
  64.             }
  65.  
  66.         } while (zahlS.isEmpty() || !umwandlungGeklappt);
  67.  
  68.         return zahlI;
  69.     }
  70.  
  71.     private static String eingabeText(String frage) {
  72.  
  73.         String eingabe;
  74.         boolean ersteDurchfuehrung = true;
  75.  
  76.         do {
  77.             String prompt = frage;
  78.             if (!ersteDurchfuehrung) {
  79.                 prompt = "Ungültige Eingabe\n" + frage;
  80.             }
  81.  
  82.             eingabe = JOptionPane.showInputDialog(null, prompt, "Text eingeben", JOptionPane.PLAIN_MESSAGE);
  83.  
  84.             if (eingabe == null) {
  85.                 System.exit(0);
  86.             }
  87.  
  88.             ersteDurchfuehrung = false;
  89.  
  90.         } while (eingabe.isEmpty());
  91.  
  92.         return eingabe;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement