0

How to calculate variables from one method inside another method: Help!!! (Complicated)

Hi everyone, This complicated plan I have is driving me nuts so I'd really appreciate if one of you superior coders could help me with this. I want the following: The main method and it's class include an array: AnimalList[] = { "frog", "dog", "rabbit", "donkey", "cow", "chicken" }; HumanList[] = { "man", "woman", "child", "grandpa", "grandma", "baby" }; ...3 Animals: String Animal1; String Animal2; String Animal3; ...and 3 Humans: String Human1; String Human2; I want to create one method, that randomly picks 3 of the array entries, compares them with the other picks to avoid duplication and than sends them back to main. Something like: for (Animal1 = AnimalList[(byte)(Math.random()*AnimalList.length), Animal2 = AnimalList[(byte)(Math.random()*AnimalList.length), Animal3 = AnimalList[(byte)(Math.random()*AnimalList.length)]; Animal1 == Animal2 || Animal1 == Animal3 || Animal 2 == Animal 3 ; Animal2 = AnimalList[(byte)(Math.random()*AnimalList.length), Animal3 = AnimalList[(byte)(Math.random()*AnimalList.length)] ); I want this to work for any other list aswell so that I can basically sent humans and the humanslist (automatically including it in .length aswell) in there so I save a lot of space in my code and it looks better sorted. In case one of you shows me how to do this... THANK YOU!!!

18th Oct 2018, 7:13 PM
Ole
3 Antworten
+ 1
String AnimalList[] = { "frog", "dog", "rabbit", "donkey", "cow", "chicken" }; String HumanList[] = { "man", "woman", "child", "grandpa", "grandma", "baby" }; String animal1 = AnimalList[(byte)(Math.random() * AnimalList.length)]; String animal2 = AnimalList[(byte)(Math.random() * AnimalList.length)]; String animal3 = AnimalList[(byte)(Math.random() * AnimalList.length)]; System.out.println(animal1 + " : " + animal2 + " = " + animal1.equals(animal2)); System.out.println(animal1 + " : " + animal3 + " = " + animal1.equals(animal3)); System.out.println(animal2 + " : " + animal3 + " = " + animal2.equals(animal3));
18th Oct 2018, 8:11 PM
Daniel (kabura)
Daniel (kabura) - avatar
+ 1
== is only for prime values (int, double ...) equal is for objects (strings)
18th Oct 2018, 8:21 PM
Daniel (kabura)
Daniel (kabura) - avatar
0
Thanks a lot for the attempt, Daniel. I didn't know about ".equals". 👍😊
18th Oct 2018, 8:17 PM
Ole