eclipse中的Java希望从字符串中创建随机 [英] Java in eclipse looking to create a random from a string

查看:112
本文介绍了eclipse中的Java希望从字符串中创建随机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待不重复字符串中任意想法的随机单词的输出?



谢谢



我的尝试:



mport java.util.Random;



公共课词

{



public static void main(String [] args){





randomWords() ;



}





public static void randomWords(){< br $>


String [] these = {This,That,Them,Time,Waster};

随机生成器=新的随机();



System.out.println(请参阅列出的5个单词中的4个);

System.out.println();

for(int i = 0; i< these.length - 1; i ++){

int nextRandom = generator.nextInt(5);



System.out.println(+ [[nextRandom]);



}

}

}

I am looking to not duplicate the output of the random words from the string any ideas?

Thanks

What I have tried:

mport java.util.Random;

public class words
{

public static void main(String[] args) {


randomWords();

}


public static void randomWords(){

String[] these = { "This", "That", "Them", "Time", "Waster"};
Random generator = new Random ();

System.out.println ("Please see listed 4 of the 5 words");
System.out.println ("");
for (int i=0; i< these.length - 1; i++) {
int nextRandom = generator.nextInt(5);

System.out.println ("" + these[nextRandom]);

}
}
}

推荐答案

你只需要保持每个返回的数字的注释,并忽略任何重复。或者,您可以在列出后删除数组中的每个单词。然后,您只需要在尝试打印之前检查它是否为空。你还需要一个不同类型的循环(),因为你很可能会比列表中的单词数多出几倍。
You just need to keep a note of each returned number and ignore any duplicates. Alternatively you could delete each word from the array after it has been listed. Then you just need to check that it is not null before trying to print it. You also need a different type of loop (while) since you will most likely go round it many more times than the number of words in your list.


这篇关于eclipse中的Java希望从字符串中创建随机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆