I am making a fortune-telling game for my school project, but I am completely new to Android Studio and Java.
At the moment, my code looks like this:
public class MainActivity extends AppCompatActivity {
public int score = 0;
int random = random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button correct = (Button) findViewById(R.id.correct);
Button other = (Button) findViewById(R.id.other);
Button newGame = (Button) findViewById(R.id.newGame);
TextView words = (TextView) findViewById(R.id.words);
}
public Integer random (){
int random = (int )(Math.random() * 5);
return random;
}
private String list[] =
{"Dog", "Cat", "Mouse", "Elephant", "Rat", "Parrot"};
public void clickedButton(View view) {
TextView words = (TextView) findViewById(R.id.words);
if (view.getId()== R.id.newGame)
{
words.setText(list[random]);
score = 0;
Log.i("score", "score = " + score);
}
if (view.getId() == R.id.correct)
{
words.setText(list[random]);
score = score +1;
Log.i("score", "score = " + score);
}
if (view.getId() == R.id.other)
{
words.setText(list[random]);
Log.i("score", "score = " + score);
}
}
}
The idea is simple. I have an array with (at the moment) 6 words in it. Since I run the game, it gives me a random word on the screen that I have to describe to others. If they guess what is right, I’ll return, I will get another word, if I can’t go and get another word ... Well, the problem that I see is the chance that I will get the same word again. So I thought that there should be a way to solve this problem.
I was thinking of adding as if statements like index 1 or 2 or 3 then gave another word, but if I had 100 words, it would be a monkey job to do this.
, , , . , .
, , Array? , , , ?