jeudi 3 septembre 2020

Not able to receive multiple different random Strings from list in Java

I want to code it so that the output of my code to output all of the Strings in a list in a random order, but not repeat and of them. I am hoping to do this with more than three variables, but to keep my question short, I only have three in this example

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class TestingOutputs {
    public static void main(String[] args) {
        boolean forever = true, one = true, two = true, three = true;
        List<String> outputsList = new ArrayList<String>();
        outputsList.add("One");
        outputsList.add("Two");
        outputsList.add("Three");

        while(forever){
            String outputsRandom = outputsList.get(new Random().nextInt(outputsList.size()));
            while(three) {
                if(outputsRandom.equals("Three")) {
                    System.out.println("This is three");
                    three = false;
                }
            while(two) {
                if(outputsRandom.equals("Two")) {
                    System.out.println("This is two");
                    two = false;
                }
            }
            while(one) {
                if(outputsRandom.equals("One")) {
                    System.out.println("This is one");
                    two = false;
                }
            }
            }
        }
        }

    }



Aucun commentaire:

Enregistrer un commentaire