[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Halp with JAVA for idiots.
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 11
Thread images: 1
File: galaxy_power-wide.jpg (2 MB, 2880x1800) Image search: [Google]
galaxy_power-wide.jpg
2 MB, 2880x1800
public class Speak
{
private static String[] words_1 = {"Mr X","Mr Y","Mr Z"};
private static String[] words_2 = {"your","my","their","the"};
private static String[] words_3 = {"best friend","lunch"};
private static String[] words_4 = {"is behind you","is crying"};

public static void main(String[] args)
{
System.out.println(getRandomString(words_1));
System.out.println(getRandomSentence());
}

static String getRandomString(String selectedArray[])
{
return selectedArray[(int) Math.random()*selectedArray.length];
}

static String getRandomSentence()
{
return (getRandomString(words_1)+", "+getRandomString(words_2)+" "+getRandomString(words_3)+" "+getRandomString(words_4));
}
}


HALP.
>>
How do I make it give me a fucking randomized sentence?
>>
Make sure your random number is less than or equal to your array size.
>>
You're casting the random number to an int before it's multiplied by the array length so it just floors to zero each time. Stick some brackets round the multiplication and it works

(int) (Math.random()*selectedArray.length)
>>
use the fucking random class with nextInt(), dumbass
>>
>>52138320
^
>>
>>52138320

I think you are pretty spot on but...

Not sure how to implement that.
>>
>>52138157
Poo in loo, Pajeet.
>>
>>52138307
this is the answer
>>
>>52141394

Yea it worked out pretty well for me.

But it isn't as clean as the nextInt()
>>
>>52141692
I prefer doing things myself. Often just less overhead.
Thread replies: 11
Thread images: 1

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.