Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Klausur - praktisch
- * Block I - Zahlenraten
- *
- * @version 1.0 from 21.11.2016
- * @author (MrMusical)
- */
- import java.util.*;
- public class Zahlenraten {
- public static void main(String[] args) {
- // Bilden der Zufallszahl
- Random rnd = new Random();
- int zufallszahl = rnd.nextInt(100);
- // Versuchszähler
- int versuche = 0;
- // Die aktuell geratene Zahl
- int rate = 0;
- while (true) {
- rate = input("Erraten Sie eine Zahl zwischen 0 und 99!", 0, 100);
- versuche++;
- if (rate == zufallszahl) {
- break;
- }
- else if (rate < zufallszahl) {
- IO.show("Die Zufallszahl ist größer als " + rate, "Zufallszahl");
- continue;
- }
- else if (rate > zufallszahl) {
- IO.show("Die Zufallszahl ist kleiner als " + rate, "Zufallszahl");
- continue;
- }
- else {
- IO.show("Dies darf nicht auftreten.");
- continue;
- }
- }
- IO.show("Sie haben gewonnen!\nDie Zufallszahl lautet " + zufallszahl + "!\nSie haben "
- + versuche + " " +((versuche == 1) ? "Versuch" : "Versuche") + " benötigt.", "Zufallszahl");
- }
- public static int input(String prompt, int min, int max) {
- int in = 0;
- while (true) {
- in = IO.getInt(prompt);
- if (min <= in && in < max) {
- // Gültige Eingabe
- break;
- }
- else {
- IO.show("Die Eingabe ist nicht gültig.", "Fehler");
- continue;
- }
- }
- return in;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement