我正在使用一个随机变量,2个字符串数组,我试图将2个字符串数组设置为具有相同的位置值,我尝试了所有可能的方法,但它们都不起作用。我是编程新手,并不完全理解java,只是tid位。我只是想知道是否可以将不同数组的整数值设置为相同。
我的代码是:
Random r1 = new Random(), r2 = new Random();
TextView txtDid, txtPhrase, txtFood, txtJokeQ, txtJokeA;
ImageView imageDonate;
int x, p, f, jq, ja;
Button btnJokeNext;
String[] JokeQArray = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
"X", "Y", "Z" };
String[] JokeAArray = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
"X", "Y", "Z" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_tab);
txtJokeQ = (TextView) findViewById(R.id.txtJokeTextQ);
txtJokeA = (TextView) findViewById(R.id.txtJokeTextA);
jq = r1.nextInt(JokeQArray.length);
r2.nextInt(JokeAArray.length);
txtJokeQ.setText(JokeQArray[jq].toString());
txtJokeA.setText(JokeAArray[jq].toString());
btnJokeNext = (Button) findViewById(R.id.btnJokeNext);
btnJokeNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
jq = r1.nextInt(JokeQArray.length);
r2.nextInt(JokeAArray.length);
txtJokeQ.setText(JokeQArray[jq].toString());
txtJokeA.setText(JokeAArray[jq].toString());
// TODO Auto-generated method stub
}
});
Thread t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(60000);
runOnUiThread(new Runnable() {
@Override
public void run() {
updateJokeTextView();
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
}
private void updateJokeTextView() {
jq = r1.nextInt(JokeQArray.length);
txtJokeQ.setText(JokeQArray[jq].toString());
ja = r1.nextInt(JokeAArray.length);
txtJokeA.setText(JokeAArray[ja].toString());
}
}
当我试图尝试不同的方式来做我想做的事情时,我实际上搞砸了代码。希望代码对你有意义,提前谢谢。
答案 0 :(得分:0)
我知道以下内容不是我的反馈意见的答案,而且观察时间太长,无法在评论部分进行描述。所以很抱歉。
无法确切地解决您的问题。 但是在完成你的代码后,我有以下的保留:
希望这能解决你的问题。如果它没有,那么请提及您的代码究竟是什么。
答案 1 :(得分:0)
由于两个阵列在同一位置具有相同的值;您可以使用 txtJokeQ.setText(JokeQArray [JQ]的ToString()); txtJokeA.setText(JokeQArray [JQ]的ToString());