首页 > 解决方案 > 如何在 Java 中保持随机化文本?

问题描述

我只是想知道,也许你能帮助我。这是代码,也许你可以编辑它。

import java.util.Scanner;
public ststic void main string(args[]){
    System.out.println("I want to keep randomize between  following texts:")
    //abc_123_def
    //jaofn_3vfdsa
    //nabdoew-8943
}

标签: javarandom

解决方案


我想这就是你要求的...

public static void main(String[] args) {
 //task: show one of these texts randomly every time the user answers "yes"
    String[] text = {"abc_123_def", "jaofn_3vfdsa", "nabdoew-8943"};
    boolean bored = false;
    Scanner s = new Scanner(System.in);

    while (!bored) {
        System.out.println("Would you like to keep randomizing?");
        if (s.nextLine().equals("yes")) {
            int rand_n = (int) Math.round(Math.random() * 2);
            System.out.println("Random text: " + text[rand_n]);
        }
        else {bored = true;}
    }
}

推荐阅读