Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Matheus G Oliveira
9,682 PointsGenerating random numbers
Hey guys,
See if you can help me. I need to generate numbers to be different, and to be random.
I can´t get different answers on these. I need the 3 answers to be different. following the code
//For the first player
String[] answersOne = {
"Space1",
"Space2",
"Space3",
"Space4",
"Space5",
};
String answerOne = "";
Random randomGeneratorOne = new Random(); //Construct number randomly
int randomNumberOne = randomGeneratorOne.nextInt(answersOne.length);
//Convert randomNumber to Text Number
answerOne = answersOne[randomNumberOne];
answerLabelOne.setText(answerOne); //Update label with dynamic answer
//For the second player String[] answersTwo= { "Space1", "Space2", "Space3", "Space4", "Space5", }; String answerRtwo = "";
Random randomGeneratorTwo = new Random();
//Construct number randomly
int randomNumberTwo = randomGeneratorTwo.nextInt(answersTwo.length);
//Convert randomNumber to Text Number
answerRtwo = answersTwo[randomNumberTwo];
answerLabelTwo.setText(answerRtwo); //Update label with dynamic answer
//For the third player
String[] answersThree = {
"Space1",
"Space2",
"Space3",
"Space4",
"Space5",
};
String answerThree = "";
Random randomGeneratorThree = new Random(); //Construct number randomly
int randomNumberThree = randomGeneratorThree.nextInt(answersThree.length);
//Convert randomNumber to Text Number
answerThree = answersThree[randomNumberThree];
answerLabelThree.setText(answerThree);
4 Answers
Victor Rodriguez
15,015 Pointsimport java.util.*;
public class random{
public static void main(String[] args){
//Answers for all players
String[] answersOne = { "Space1", "Space2", "Space3", "Space4", "Space5", }; String answerOne = "";
String[] answersTwo = { "Space1", "Space2", "Space3", "Space4", "Space5", }; String answerTwo = "";
String[] answersThree = { "Space1", "Space2", "Space3", "Space4", "Space5", }; String answerThree = "";
//create random for each player
int randomNumberOne = getRandom();
int randomNumbeTwo = getRandom();
int randomNumberThree = getRandom();
//Convert randomNumber to Text Number
answerOne = answersOne[randomNumberOne];
answerTwo = answersTwo[randomNumbeTwo];
answerThree = answersTwo[randomNumberThree];
//Update label with dynamic answer
System.out.println(answerOne);
System.out.println(answerTwo);
System.out.println(answerThree);
}
public static int getRandom(){
Random rand = new Random();
int value = rand.nextInt(5);
return value;
}
Not sure if this is what you were going for. but it may help.
Matheus G Oliveira
9,682 PointsUau man. Really appreciated for that! I´ve got the idea and it helped a lot to organize my code.
Now let´s go for the issues:
1 - I´ve created a new class: randomActivity to put the code youve sent me, that is right or i have to put on the MainActivity?
2 - I needed the random Answer to appear on 3 different TextView when i click a button, and that´s not happening. I declared view variables on the onCreate method and set the answer to the respective TextView. But still its not showing
Victor Rodriguez
15,015 Pointsfeel free to use the format you want. If you put it in a separate class remember that you have to instantiate it and have a getter method for returning the values you need from the class.
class RandomActivity(){
//code.....
public String getAnswer(){ //this returns a string but can return whatever you need it too. if you need more,
return myAnswer; //make another getter to return other things
}
}
class Main{
public static void main(String[] args){
RandomActivity random = new RandomActivity();
String answer = random.getAnswer(); //this will call the getAnswer method from RandomActivity class
} //and set answer to the returned value
}
from there you can reuse random.getAnswer(); to get more randomAnswers. then create your view variables in main and send them to the 3 textviews. After that, i'd have to see more of your code to debug the problems with why it is not showing.
Matheus G Oliveira
9,682 PointsCool, so let´s try it out:
Here it goes my MainActivity class:
package com.example.pp;
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.View; import android.widget.Button; import android.widget.TextView;
public class MainActivity extends Activity {
public static void main(String[] args){
randomActivity random = new randomActivity();
String answersOne = random.getAnswer();
String answersTwo = random.getAnswer();
String answersThree = random.getAnswer(); //this will call the getAnswer method from RandomActivity class
} //and set answer to the returned value
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Declare View variables
final TextView answerLabelOne1 = (TextView) findViewById(R.id.plrOne);
final TextView answerLabelTwo1 = (TextView) findViewById(R.id.plrTwo);
final TextView answerLabelThree1 = (TextView) findViewById(R.id.plrThree);
final Button getAnswerButton = (Button) findViewById(R.id.btnGo);
getAnswerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
/* if checkboxRANK is checked, initialize random for spaces
- if checkboxPLAYER is checked, count the number and return an integer
- when button is pressed, return a value name of player + randomSpace
-
*/
};});
} @Override public boolean onCreateOptionsMenu(Menu menu){ MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; } }
Matheus G Oliveira
9,682 PointsAnd the randomActivity class:
import java.util.*;
public class random{ public static void main(String[] args){
//Answers for all players String[] answersOne = { "Space1", "Space2", "Space3", "Space4", "Space5", }; String answerOne = ""; String[] answersTwo = { "Space1", "Space2", "Space3", "Space4", "Space5", }; String answerTwo = ""; String[] answersThree = { "Space1", "Space2", "Space3", "Space4", "Space5", }; String answerThree = "";
//create random for each player
int randomNumberOne = getRandom();
int randomNumbeTwo = getRandom();
int randomNumberThree = getRandom();
//Convert randomNumber to Text Number
answerOne = answersOne[randomNumberOne];
answerTwo = answersTwo[randomNumbeTwo];
answerThree = answersTwo[randomNumberThree];
//Update label with dynamic answer
System.out.println(answerOne);
System.out.println(answerTwo);
System.out.println(answerThree);
}
/* public static int getRandom(){ Random rand = new Random(); int value = rand.nextInt(5); return value; }
//player selection
//with CheckBox
public static int getPlayers(){ String[] playersCount = {"plr1", "plr2", "plr3"}; String playerCount = ""; return; }
*/
public String getAnswer(){ //this returns a string but can return whatever you need it too. if you need more,
return "";
//make another getter to return other things
}}