Jac*_*dge 0 java random android numbers
我正在制作一个应用程序,下面的代码应该生成7个随机数并将它们打印到屏幕上,并且所有数字都不应该与另一个相同.
我已经尝试过各种方式.我花了大约5个小时检查它,得到第二个意见,他们说它应该工作,但事实并非如此.它有什么作用?它只是将7个随机数写入屏幕,但大部分时间都有重复,大部分时间应用程序停止(错误),但代码中没有错误.这只是我尝试过的许多方法中的一种方式.有人可以告诉我,如果有什么不对吗?还是有建议?还是那么善意给我一些示例代码来替换while循环?任何建议都会很棒,请记住我对java很新.
更新:while循环是主循环.它调用一个数字方法并制作7个随机数并将它们存储在"text_counter"数组中,然后我检查它们是否是数组中的重复数字,如果没有,它会突破循环并将它们打印到屏幕上,但是如果它有回到循环的开头以生成更多随机数并再次检查它们.
这是代码
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Lotto extends Activity {
/** Called when the activity is first created. */
Random dice = new Random();
TextView top, med, and, qcounter, wcounter, ecounter, rcounter, tcounter,
ycounter, ucounter;
@SuppressWarnings("rawtypes")
Set uniqueItems = new HashSet();
Button gen;
EditText input1, input2;
int[] text_counter = { 1, 2, 3, 4, 5, 6, 7 };
boolean o = false;
boolean oo = false;
boolean ooo = false;
int text1;
int pre_text2;
int count = 0;
int text2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lotto_xml);
gen = (Button) findViewById(R.id.b_gen);
// ////////////////////////////////////// text view var
qcounter = (TextView) findViewById(R.id.q_lotto);
wcounter = (TextView) findViewById(R.id.w_lotto);
ecounter = (TextView) findViewById(R.id.e_lotto);
rcounter = (TextView) findViewById(R.id.r_lotto);
tcounter = (TextView) findViewById(R.id.t_lotto);
ycounter = (TextView) findViewById(R.id.y_lotto);
ucounter = (TextView) findViewById(R.id.u_lotto);
// ////////////////////////////////////////////////////
final EditText input1 = (EditText) findViewById(R.id.et_min);
final EditText input2 = (EditText) findViewById(R.id.et_max);
gen.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings({ "unchecked" })
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
o = false;
oo = false;
count = 0;
while (o == false) {
number();
count = 0;
for (int i = 0; i < text_counter.length; i++) {
if (!uniqueItems.add(text_counter[i])) {
count = count + 1;
}
}
if (count == 0) {
o = true;
}
}
qcounter.setText(String.valueOf(text_counter[0]) + ",");
wcounter.setText(String.valueOf(text_counter[1]) + ",");
ecounter.setText(String.valueOf(text_counter[2]) + ",");
rcounter.setText(String.valueOf(text_counter[3]) + ",");
tcounter.setText(String.valueOf(text_counter[4]) + ",");
ycounter.setText(String.valueOf(text_counter[5]) + ",");
ucounter.setText(String.valueOf(text_counter[6]));
}
private void number() {
// TODO Auto-generated method stub
text1 = Integer.parseInt(input1.getText().toString());
text2 = Integer.parseInt(input2.getText().toString());
text2 = text2 - text1;
text2 = text2 + 1;
for (int i = 0; i < 7; i++) {
text_counter[i] = text1 + dice.nextInt(text2);
}
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
你永远不会清除调用的HashSet uniqueItems.你应该在每个循环之前清除它,否则你将永远在那里累积数字.
| 归档时间: |
|
| 查看次数: |
2106 次 |
| 最近记录: |