Advertisement
MrMusical

Klausur: Block I - Zahlenraten

Dec 9th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. /**
  2.   * Klausur - praktisch
  3.   * Block I - Zahlenraten
  4.   *
  5.   * @version 1.0 from 21.11.2016
  6.   * @author (MrMusical)
  7.   */
  8.  
  9. import java.util.*;
  10.  
  11. public class Zahlenraten {
  12.  
  13.   public static void main(String[] args) {
  14.     // Bilden der Zufallszahl
  15.     Random rnd = new Random();
  16.     int zufallszahl = rnd.nextInt(100);
  17.     // Versuchszähler
  18.     int versuche = 0;
  19.     // Die aktuell geratene Zahl
  20.     int rate = 0;
  21.    
  22.     while (true) {
  23.       rate = input("Erraten Sie eine Zahl zwischen 0 und 99!", 0, 100);
  24.       versuche++;
  25.       if (rate == zufallszahl) {
  26.         break;
  27.       }
  28.       else if (rate < zufallszahl) {
  29.         IO.show("Die Zufallszahl ist größer als " + rate, "Zufallszahl");
  30.         continue;
  31.       }
  32.       else if (rate > zufallszahl) {
  33.         IO.show("Die Zufallszahl ist kleiner als " + rate, "Zufallszahl");
  34.         continue;
  35.       }
  36.       else {
  37.         IO.show("Dies darf nicht auftreten.");
  38.         continue;
  39.       }
  40.     }
  41.     IO.show("Sie haben gewonnen!\nDie Zufallszahl lautet " + zufallszahl + "!\nSie haben "
  42.              + versuche + " " +((versuche == 1) ? "Versuch" : "Versuche") + " benötigt.", "Zufallszahl");
  43.   }
  44.  
  45.   public static int input(String prompt, int min, int max) {
  46.     int in = 0;
  47.     while (true) {
  48.       in = IO.getInt(prompt);
  49.       if (min <= in && in < max) {
  50.         // Gültige Eingabe
  51.         break;
  52.       }
  53.       else {
  54.         IO.show("Die Eingabe ist nicht gültig.", "Fehler");
  55.         continue;
  56.       }
  57.     }
  58.     return in;
  59.   }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement