我有两个不同的随机数生成器:“numtofind”和“num”。第一个将1到50之间的数字放入textView("textNumberToHit")
,第二个将1到50之间的数字放到另外10个textViews("textGeneratenumber1-12")
。
当游戏开始时,我希望第一个textView("textNumberToHit")
包含在其他textView("textGeneratenumber")
个元素之一中。也就是说,如果textView("textNumberToHit")
为17,则17应出现在其他元素之一中。我希望在按下“buttonGenerate”按钮时发生这种情况。
虽然我可以按下按钮,但我无法找到执行上述逻辑的方法。
以下是textNumberToHit的代码:
final Random numtofind = new Random();
final TextView textNumberToHit = (TextView) findViewById(R.id.numbertofind);
textNumberToHit.setText(String.valueOf(numtofind.nextInt(51)));
以下是textGeneratenumber的代码:
final Random num = new Random();
buttongenerate.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
buttongenerate.setBackgroundResource(R.drawable.pressedbut);
textGenerateNumber1.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber2.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber3.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber4.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber5.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber6.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber7.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber8.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber9.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber10.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber11.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber12.setText(String.valueOf(num.nextInt(51)));
break;
case MotionEvent.ACTION_UP:
buttongenerate.setBackgroundResource(R.drawable.normalbut);
break;
}
return true;
}
});
这是一张图片供您更好地理解:
我将如何做到这一点?
答案 0 :(得分:2)
以这种方式思考。这是一个基于您要求的(简化)程序。
问:该程序描述中的逻辑/数学缺陷是什么? 答:显然第二个数字不是随机数!
所以这就是你需要做的。 (简化版!)
....或类似的东西。
1 - 至少,我认为他们需要与众不同。如果偶尔看到"号码被发现,你的游戏看起来会很奇怪。在多个按钮....
答案 1 :(得分:0)
我建议首先使用的是使用GridView的12个静态TextView:https://developer.android.com/guide/topics/ui/layout/gridview.html
用你所拥有的东西回答你的问题;这就是我如何处理这个问题:
步骤:
代码:
final Random numtofind = new Random();
final int numtofindValue = numtofind.nextInt(51);
final TextView textNumberToHit = (TextView) findViewById(R.id.numbertofind);
textNumberToHit.setText(String.valueOf(numtofindValue));
final Random num = new Random();
final Random textViewToSet = new Random();
// reason i am adding 1 is because nextInt(int num) returns a random number from 0 up to but not including the value of the param "num"
// http://docs.oracle.com/javase/6/docs/api/java/util/Random.html#nextInt(int)
// and since you have 12 textviews and we want to get one of those we need to add one so the range of random numbers will be 1-12 instead of 0-11.
int textViewToSetValue = textViewToSet.nextInt(12) + 1; // which textview to set the value of numtofind
// populate hashmap with textviews
Map<Integer, TextView> textViews = new HashMap<>();
// not an efficient way of doing this - but since you are not using a GridView its the only way
textViews.put(1, textGenerateNumber1);
textViews.put(2, textGenerateNumber2);
textViews.put(3, textGenerateNumber3);
textViews.put(4, textGenerateNumber4);
textViews.put(5, textGenerateNumber5);
textViews.put(6, textGenerateNumber6);
textViews.put(7, textGenerateNumber7);
textViews.put(8, textGenerateNumber8);
textViews.put(9, textGenerateNumber9);
textViews.put(10, textGenerateNumber10);
textViews.put(11, textGenerateNumber11);
textViews.put(12, textGenerateNumber12);
TextView tvToSet = textViews.get(textViewToSetValue); // this will get the textview for a number 1 - 12
// set value of textView
tvToSet.setText(String.valueOf(numtofindValue));
buttongenerate.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
buttongenerate.setBackgroundResource(R.drawable.pressedbut);
// loop through the remaining textViews and set their values to random values
for (Map.Entry<Integer, TextView> entry : textViews.entrySet(){
// make sure we dont reset the value we set earlier
if(entry.getKey() != textViewToSetValue){
entry.getValue().setText(num.nextInt(51));
}
}
break;
case MotionEvent.ACTION_UP:
buttongenerate.setBackgroundResource(R.drawable.normalbut);
break;
}
return true;
}
});
答案 2 :(得分:0)
首先,您要将生成的数字的值存储为:
final Random numtofind = new Random();
final TextView textNumberToHit = (TextView) findViewById(R.id.numbertofind);
int generatedNumber = numtofind.nextInt(51);
textNumberToHit.setText(String.valueOf(generatedNumber));
现在,您为textGenerateNumber生成了12个其他随机数。 它有以下问题:
所以,首先我会创建12个独特的random。
ArrayList<Integer> wrongNumbers = new ArrayList<Integer>();
int wrongNumber = 0;
//I will not write the exact code. You will need to do them but the logic is correct.
for(loop from 1 to 50) //you would not be looping 50 times. It is very unlikely that you won't get 12 random numbers even in 50 times (worst case)
{
wrongNumber = numtofind.nextInt(51);
if(wrongNumber DOES NOT EXIST IN wrongNumbers)
{
wrongNumber => (PUSH TO ARRAY(wrongNumbers))
}
if(wrongNumbers.SIZE => 12) //You know have 12 numbers
{exit from this for loop;}
}
//Now create the textGenerateNumbers in loop.
buttongenerate.setBackgroundResource(R.drawable.pressedbut);
for(loop from int i = 0 => 11)
{
textGenerateNumber[i + 1].setText(wrongNumbers[i]); //as your textGenerateNumber is from 1-12
}
//Create a random value from 0-11 to hold the correct value.
int correctIndex = numtofind.nextInt(11);
//Finally, override the value at the correctIndex with your number to hit.
textGenerateNumber[correctIndex] = generatedNumber;