[JAVA] I checked Zenzenzense, not Zenzenzense

I'm learning Java at a training school, so I made it by practicing lists and conditional branching.


import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class zensecheck {
	public static void main(String[] args) {

		System.out.println("your");

		//List for comparison
		String correctStr = "Zenzenzense";
		//A list that outputs characters at random
		String[] words = {"Before","world"};

		//Initialize list
		List<String> list = new LinkedList<>();

		Random r = new Random();

		//Empty array for storage
		String str = "";
		String word = "";

		while(true) {

			//word Previous / world is stored randomly
			word = words[r.nextInt(2)];
			System.out.println(word);
			list.add(word);

			//If the length of list exceeds 4, remove the 0th element with remove
			if(list.size() > 4) {
				list.remove(0);
			}

			//Store the last 4 times before and the world in str
			str = list.stream().collect(Collectors.joining());

			//If the contents of str and correctStr are the same, display the lyrics and exit the loop
			if(str.equals(correctStr)) {
				System.out.println("So I started looking for you");
				break;
			}
		}
	}
}

It was a little crazy in the class

Recommended Posts

I checked Zenzenzense, not Zenzenzense
I checked this
Circuit Breaker Pattern I checked
I checked Java Flight Recorder (JFR)