如何在Java中保持文本随机化?
我只是想知道,也许你能帮我。 这是代码,也许你可以编辑它如何在Java中保持文本随机化?,java,random,Java,Random,我只是想知道,也许你能帮我。 这是代码,也许你可以编辑它 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 } 您可以将不同的字符
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
}
您可以将不同的
字符串
存储在列表
中,然后将其洗牌
System.out.println("I want to keep randomize between following texts:");
List<String> texts = Arrays.asList("abc_123_def", "jaofn_3vfdsa", "nabdoew-8943");
Collections.shuffle(texts);
System.out.println(texts); // [jaofn_3vfdsa, abc_123_def, nabdoew-8943]
Collections.shuffle(texts);
System.out.println(texts); // [abc_123_def, nabdoew-8943, jaofn_3vfdsa]
我想这就是你想要的
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;}
}
}
一个快速的谷歌搜索,你可以找到一些文章。像这样->
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;}
}
}