if-Bedingungen

Jurow75

Mitglied
Hallo,
im folgenden Code von Zeile 20 - 34 werden die Blöcke der if-Anweisungen immer ausgeführt, obwohl das Ergebnis false ist. Ich hoffe Ihr könnt mir helfen, ich sitze nämlich schon seit Stunden an der Fehlersuche...

Java:
package aufgaben;

import java.util.Scanner;

public class SchingSchangSchong {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Herzlich willkommen zu Schere, Stein, Papier. Wie viele Runden möchtest du spielen?");
      
        int rounds = sc.nextInt();
        Scanner scan = new Scanner(System.in);
      
        hell:
        for(int aktuellRound = 1; aktuellRound <= rounds; aktuellRound++) {
            System.out.println("Schere, Stein oder Papier?");
            String user = scan.nextLine();
          
            System.out.println(! user.equals("Schere"));    /**Nur ein Test auf die Bedingung*/
            if( ! user.equals("Schere")) {
                System.err.println("Ungültige Eingabe!");
                aktuellRound--;
                continue hell;
            }
            if(! user.equals("Stein")) {
                System.err.println("Ungültige Eingabe!");
                aktuellRound--;
                continue hell;
            }
            if(! user.equals("Papier")) {
                System.err.println("Ungültige Eingabe!");
                aktuellRound--;
                continue hell;
            }
            int bot = (int)(Math.random()*3);
            String ai;
          
            if(bot == 0)
                ai = "Schere";
            else if(bot == 1)
                ai = "Stein";
            else
                ai = "Papier";
          
            if(user.equals(ai)) {
                System.out.println("Unentschieden! Der Bot hat auch " + user);
                continue hell;
            }
            //man geht vom user aus
            if(user.equals("Schere")) {
                if(ai.equals("Stein"))
                       System.out.println("Du hast verloren! Stein Schlägt Schere.");
                else
                    System.out.println("Du hast gewonnen! Schere zerschneidet Papier.");
            }
            if(user.equals("Stein")) {
                if(ai.equals("Schere"))
                    System.out.println("Du hast gewonnen! Stein schlägt Schere.");
                else
                    System.out.println("Du hast verloren! Papier umwickelt Stein.");
            }
            if(user.equals("Papier")) {
                if(ai.equals("Schere"))
                    System.out.println("Du hast verloren! Schere schneidet Papier.");
                else
                    System.out.println("Du hast gewonnen! Papier umwickelt Stein.");
            }
        }
        System.out.println("Bis zum nächsten Mal!");
    }
}
 
Zuletzt bearbeitet von einem Moderator:

Java xyrse123

Bekanntes Mitglied
So müsste es gehen, denn alle drei Bedingungen müssen ja erfüllt sein:
Java:
 if(!user.equals("Schere") && !user.equals("Papier") && !user.equals("Stein")) {
                System.err.println("Ungültige Eingabe!");
                aktuellRound--;
                continue;
            }
 

Blender3D

Top Contributor
if( ! user.equals("Schere")) {
System.err.println("Ungültige Eingabe!");
aktuellRound--;
continue hell;
}
if(! user.equals("Stein")) {
System.err.println("Ungültige Eingabe!");
aktuellRound--;
continue hell;
}
if(! user.equals("Papier")) {
System.err.println("Ungültige Eingabe!");
aktuellRound--;
continue hell;
}
int bot = (int)(Math.random()*3);
String ai;

if(bot == 0)
ai = "Schere";
else if(bot == 1)
ai = "Stein";
else
ai = "Papier";
…….
Wenn Du das ganze mit Objekten z.B. Hand für die gewählte Hand ( Schere, Stein , Papier) aufteilen würdest.
Kann man sich den ganzen if then else Dschungel ersparen.
Das Prinzip lautet Teile und Herrsche.
Java:
public class Hand implements Comparable<Hand> {
    public static enum TYPES {
        SCHERE, STEIN, PAPIER
    };

    private TYPES type = null;

    public Hand(String type) throws IllegalArgumentException {
        TYPES[] types = TYPES.values();
        type = type.toUpperCase();

        for (int i = 0; i < types.length; i++) {
            if (types[i].toString().equals(type)) {
                this.type = types[i];
                break;
            }
        }
        if (this.type == null)
            throw new IllegalArgumentException("Unknown type ");
    }

    public Hand(int id) throws IllegalArgumentException {
        if (id < 0 || id > TYPES.values().length)
            throw new IllegalArgumentException("Unknown type ");
        type = TYPES.values()[id];
    }

    @Override
    public int compareTo(Hand o) {
        if (type.equals(o.type))
            return 0;
        switch (type) {
        case PAPIER:
            return o.type.equals(TYPES.SCHERE) ? -1 : 1;
        case SCHERE:
            return o.type.equals(TYPES.STEIN) ? -1 : 1;
        case STEIN:
        }
        return o.type.equals(TYPES.PAPIER) ? -1 : 1;
    }

    public TYPES getType() {
        return type;
    }

    @Override
    public String toString() {
        return type.toString();
    }
}
Java:
import java.util.Random;
import java.util.Scanner;

public class SchingSchangSchong {
    final static int PLAYER = 0;
    final static int CPU = 1;
    final static Random rnd = new Random(System.currentTimeMillis());

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Herzlich willkommen zu Schere, Stein, Papier. Wie viele Runden möchtest du spielen?");

        Hand[] hand = new Hand[2];
        int[] points = new int[2];
        int rounds = scanner.nextInt();
        scanner.nextLine();

        for (int i = 0; i < rounds; i++) {
            System.out.println("-----\tRunde\t<" + (i + 1) + ">\t-----");
            hand[PLAYER] = inputPlayerHand(scanner);
            hand[CPU] = new Hand(rnd.nextInt(3));
            int cmp = hand[PLAYER].compareTo(hand[CPU]);
            evaluateRound(hand, points, cmp);
        }
        System.out.println("\nPunkte Spieler\t" + points[PLAYER]);
        System.out.println("Punkte CPU\t" + points[CPU]);
        System.out.println("Unentschieden\t" + (rounds - points[CPU] - points[PLAYER]));
    }

    private static void evaluateRound(Hand[] hand, int[] points, int cmp) {
        if (cmp == 0) {
            System.out.println(hand[PLAYER] + " < > " + hand[CPU] + " Unentschieden!");
            return;
        }
        if (cmp < 0) {
            points[CPU]++;
            System.out.println(hand[PLAYER] + " verliert gegen " + hand[CPU] + "!");

        } else {
            points[PLAYER]++;
            System.out.println(hand[PLAYER] + " schlaegt " + hand[CPU] + "!");
        }
    }

    private static Hand inputPlayerHand(Scanner scanner) {
        Hand hand = null;
        while (hand == null) {
            try {
                hand = new Hand(scanner.nextLine().trim());
            } catch (IllegalArgumentException e) {
                System.err.println("Nur Schere, Stein Papier erlaubt!");
            }
        }
        return hand;
    }
}
 

Ähnliche Java Themen

Neue Themen


Oben