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:
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:
- 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;
}
}