mardi 30 janvier 2018

Trying to limit input options for hangman

I was given a almost complete code (with a Library i downloaded). The code I'm making should allow a user to input letters, 1 capital letter at a time. I created a while-loop that checks if the characters that are input are ok. Problem is you can "break" the code, i guess cause the if chunks are connected? would love some help.

    import se.rosendalsgymnasiet.hangman.HangmanGameEngine;

import java.util.Scanner;

public class Thomas_Parker_hangman_1 {
public static void main(String[] args) {
    String previousLetters = "";
    HangmanGameEngine hangman = new HangmanGameEngine();
    hangman.selectWord();
    hangman.createStatusString();
    while (!hangman.evaluateStatus(previousLetters)) {
        char letter;

//Nedanstående metod saknar definition, det är din uppgift att skapa metoden
            letter = guess(previousLetters);

            previousLetters = previousLetters + String.valueOf(letter);
            hangman.updateStatusString(letter);
        }
        System.out.println("Bravo, du lyckades finna rätt ord!");
    }

/**
 * Låter användaren skriva in bokstäver
 *
 * @param previousguesses De gissningar användaren prövat
 * @return
 */

This is what i have made and where i need help:

private static char guess(String previousguesses) {
        Scanner keyboard = new Scanner(System.in);
        String userguess = keyboard.next().toUpperCase();
    char Charactercontroll;
    Charactercontroll = userguess.charAt(0);


    boolean Supercontroll = true;


    while (Supercontroll = true) {
        if (userguess.length() >= 2) {
            System.out.println("Du får bara skriva en bokstav, försök igen!");
            Supercontroll=true;
            userguess = keyboard.next().toUpperCase();
        } else if (previousguesses.contains(String.valueOf(userguess))) {
            System.out.println("Du har redan gissat på denna bokstav, försök igen!");
            Supercontroll=true;
            userguess = keyboard.next().toUpperCase();
        } else if (!Character.isLetter(Charactercontroll)) {
            System.out.println("Detta är inte en bokstav, försök igen!");
            Supercontroll=true;
            userguess = keyboard.next().toUpperCase();
        } else {
            Supercontroll = false;
        }
        while (Supercontroll=false);
        return userguess.charAt(0);
    }
    return userguess.charAt(0);


}

}

Aucun commentaire:

Enregistrer un commentaire