我正在为我的School项目制作猜谜游戏,但我对Android Studio和Java颇为陌生。Android Studio - Array,ArrayList,List - JAVA
目前我的代码看起来是这样的:
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)
{
// set maybe new array so new textview does not give the same value
words.setText(list[random]);
score = score +1;
Log.i("score", "score = " + score);
}
if (view.getId() == R.id.other)
{
// set maybe new array so new textview does not give the same value
words.setText(list[random]);
Log.i("score", "score = " + score);
}
}
}
的想法很简单。我有我的阵列(目前)6个字。所以当我启动游戏时,它给了我一个随机的单词,我必须向其他人描述。如果他们猜对了,我按正确,我会得到另一个词,如果不是,我可以通过,并得到另一个词......呃,我看到的问题是,我有机会再次获得相同的单词。所以我认为应该有办法解决这个问题。
我想添加像if语句一样,如果索引1或2或3然后给另一个单词,但如果我有100个单词,这将是猴子的工作来做到这一点。
所以我认为应该有一个机会,我可以如何从类似的温度删除单词。数组,但没有内置的方法来完成它。
我读的是有那些arrayLists和列表,但不是更容易使用数组?也许一些提示如何?
为工作选择合适的工具很重要。 'Array'是固定长度的,而'ArrayList'允许你扩展它并且(低效地)从中间删除条目。一个'LinkedList'可以有效地让你删除任何条目,同时可以根据需要增长和缩小。 – hexafraction
就我个人而言,我会将数组混合起来*(使用随机交换位置)*,然后只保留一个“索引”来指明您所在的单词,因此您不必跟踪数组中已经经过了哪些单词。 –
@RileyCarney你的意思是 - 我的随机数是3,然后我将[3]和[1]与地点互换。而对于下一个随机我只是添加变量x,这将是1,在每次“正确”或“通过”被按下后增加? – osiic21