Strings zusammenlegen

Kirby.exe

Top Contributor
Also wir sollen ein Programm schreibt, dass im Grunde wie Hangman funktioniert:
- Buchstabe soll eingegeben werden
- Es wird geprüft ob der Buchstabe im Wort vorkommt wenn ja, dann soll er aus gegeben werden.

Z.B. so:

Code:
String Wort = "test";
eingabe = "t";
Ausgabe = "t__t";

Das Problem in meinem Fall wie schaffe ich es dass die Einträge vom ersten Durchlauf immer gemeinsam geprintet werden? Ich möchte halt z.B wenn beim zweiten durchlauf die eingabe "e" getätigt wird diese Ausgabe ---> "te_t".

Ich hatte mir dazu folgenden Code überlegt:
Java:
import java.io.*;
import java.util.Arrays;

public class Wörterraten {

    public static void main(String[] args) throws IOException{
        String word = "oop 1";
        String print = "";
        int maxTrys = 3;
        int counter = 0;
        while(counter != maxTrys) {
            int [] index = wordCheck(word);
            print = arrayStepper(index, word);
            System.out.println(print);
            counter++;
         
            //word = print;
         
        }
        System.out.println("All your trys have been used up");
    }
 
    public static String input() throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter your Guess: ");
        String word = in.readLine();
        return word;
    }
 
    public static String arrayStepper(int [] arr, String word) {
        String result = "";
        for(int i = 0; i < word.length(); i++) {
            boolean temp = realityCheck(arr, i);
                if(temp == true) {
                    result += word.charAt(i);
                }else if(temp == false){
                    result += "_";
                }
        }
     
        return result;
    }
 
    public static boolean realityCheck(int[] arr, int targetValue) {
        for(int count: arr){
            if(count == targetValue)
                return true;
        }
        return false;
    }
 
    public static int [] wordCheck(String a) throws IOException{
        String s = input();
        int [] returner = new int[a.length()];
        int count = 0;
        for(int j = 0; j < s.length(); j++) {
            char index = s.charAt(j);
            for (int i = 0; i < a.length(); i++) {
                if (a.charAt(i) == index) {
                    returner[count] = i;
                    count++;
                }
            }
        }
    returner = Arrays.copyOf(returner, count);
    return returner;
    }

}
 

MoxxiManagarm

Top Contributor
Java ist eine objektorientierte Sprache und du solltest dir deine Domäne auch als solches vorstellen. Die ganzen static Methoden gehören hier nicht hin. Du hast eher ein Script geschrieben.

So könnte deine Klasse aussehen:

Java:
class Hangman {
  private static int MAX_GUESSES = 5;

  private String solution;
  private String current;
  private int wrongGuesses;

  private Hangman(String solution) {
    this.solution = solution;
    this.current = solution.replaceAll(".", "_");
    wrongGuesses = 0;
  }

  public static Hangman createNewGame() {
    String solution = /* Random Wort Generierung*/;
    return new Hangman(solution);
  }

  public /*sinnvoller return type*/ guessCharacter(char c) {
    // modifiziere current mit c anhand solution
  }

  public String getCurrent() {
    return current;
  }

  public String getWrongGuesses() {
    return wrongGuesses;
  }

  public int guessesLeft() {
    return MAX_GUESSES - wrongGuesses;
  }

  public boolean isWon() {
    return solution.equals(current);
  }

  // ...
}
 
Zuletzt bearbeitet:

Kirby.exe

Top Contributor
Ich muss gestehen, dass ich noch sehr wenig Erfahrung habe mit Benutzung von eigen Datentypen :( Hatte es in der letzten Vorlesung angeschnitten.
 

Neue Themen


Oben