在括号内
public class MainActivity extends AppCompatActivity
我(除其他外):
public float numAlea() {
return r.nextFloat();
}
在OnCreate里面我有:
r = new Random();
然后,我有一个私有的void方法,开头
random0to1 = numAlea();
并使用'random0to1'就好像它是一个随机数,但似乎这个random0to1总是一个介于2/3和1之间的数字,因为每次调用这个私有void方法时应该随机“弹出”的三个按钮之间第三个按钮是“弹出”。这段代码有什么问题?
非常感谢您的宝贵时间!
我相信这个问题与Stack Overflow建议的问题不同,因为我没有使用种子(我想我不需要,因为我曾经在构建另一个应用程序并设法修复它时遇到过类似的问题 - 不幸的是,使用该应用程序的相同结构的随机数字并没有解决我在这里提出的问题)。
更新:
main activity.Java
package com.example.android.whack_a_lock_022;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
int cl = 0;
int nl = 0;
int sl = 0;
int whacks = 0;
int maxwhacks = 0;
Random r;
float random0to1;
private CountDownTimer countDownTimer;
private Button start;
private Button cancel;
private TextView time;
public float numAlea() {
return r.nextFloat();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
r = new Random();
Button clButton = findViewById(R.id.cl);
Button nlButton = findViewById(R.id.nl);
Button slButton = findViewById(R.id.sl);
View.OnClickListener btnClickListener = new View.OnClickListener () {
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.startButton :
start();
break;
case R.id.cancelButton :
cancel();
break;
}
}
};
clButton.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cl == 1) {
cl = 0;
TextView CapsLock = findViewById(R.id.cl);
CapsLock.setText("0");
whacks++;
TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
NumberOfWhacks.setText(String.valueOf(whacks));
if (whacks > maxwhacks) {
maxwhacks = whacks;
TextView NumberOfMaxWhacks = findViewById(R.id.numberOfMaxWhacksView);
NumberOfMaxWhacks.setText(String.valueOf(maxwhacks));
}
pop();
}
}
});
nlButton.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
if (nl == 1) {
nl = 0;
TextView NumLock = findViewById(R.id.nl);
NumLock.setText("0");
whacks++;
TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
NumberOfWhacks.setText(String.valueOf(whacks));
if (whacks > maxwhacks) {
maxwhacks = whacks;
TextView NumberOfMaxWhacks = findViewById(R.id.numberOfMaxWhacksView);
NumberOfMaxWhacks.setText(String.valueOf(maxwhacks));
}
pop();
}
}
});
slButton.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sl == 1) {
sl = 0;
TextView ScrollLock = findViewById(R.id.sl);
ScrollLock.setText("0");
whacks++;
TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
NumberOfWhacks.setText(String.valueOf(whacks));
if (whacks > maxwhacks) {
maxwhacks = whacks;
TextView NumberOfMaxWhacks = findViewById(R.id.numberOfMaxWhacksView);
NumberOfMaxWhacks.setText(String.valueOf(maxwhacks));
}
pop();
}
}
});
// Capture our button from layout
start = (Button) findViewById(R.id.startButton);
start.setOnClickListener(btnClickListener);
cancel = (Button) findViewById(R.id.cancelButton);
cancel.setOnClickListener(btnClickListener);
time = (TextView) findViewById(R.id.time);
// Register the onClick listener with the implementation above
}
private void start () {
time.setText("60");
countDownTimer = new CountDownTimer(60 * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
time.setText("" + millisUntilFinished / 1000);
}
@Override
public void onFinish(){
time.setText("Done !");
}
};
countDownTimer.start();
pop();
}
private void pop(){
// Escolhe e "POPa" o botão
// Gera número aleatório
random0to1 = numAlea();
if (random0to1 <= (1 / 3)) {
cl = 1;
Button clButton = (Button) findViewById(R.id.cl);
clButton.setText("1");
} else if (random0to1 <= 2 / 3) {
nl = 1;
Button nlButton = (Button) findViewById(R.id.nl);
nlButton.setText("1");
} else if (random0to1 <= 1) {
sl = 1;
Button slButton = (Button) findViewById(R.id.sl);
slButton.setText("1");
}
}
private void cancel() {
if (countDownTimer != null) {
countDownTimer.cancel();
countDownTimer = null;
}
//Põe botões a zeros
cl = 0;
Button clButton = (Button) findViewById(R.id.cl);
clButton.setText("0");
nl = 0;
Button nlButton = (Button) findViewById(R.id.nl);
nlButton.setText("0");
sl = 0;
Button slButton = (Button) findViewById(R.id.sl);
slButton.setText("0");
whacks = 0;
TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
NumberOfWhacks.setText("0");
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/whacksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="WHACKS"
android:textSize="24sp"
android:textAlignment="center"/>
<TextView
android:id="@+id/maxWhacksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="MAX WHACKS"
android:layout_weight="1"
android:textSize="24sp"/>
</LinearLayout>
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="60"
android:textSize="24sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/numberOfWhacksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"
android:textAlignment="center"
android:textSize="24sp"/>
<TextView
android:id="@+id/numberOfMaxWhacksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="0"
android:layout_weight="1"
android:textSize="24sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/cl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_weight="1"/>
<Button
android:id="@+id/nl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_weight="1"/>
<Button
android:id="@+id/sl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_weight="1"/>
</LinearLayout>
<Button
android:id="@+id/startButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Start" />
<Button
android:id="@+id/cancelButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
我认为代码的这一部分存在错误 - 请参阅我的评论:
private void pop(){
random0to1 = numAlea();
if (random0to1 <= (1 / 3)) { // 1/3 == 0 , try to use 1F / 3
cl = 1;
Button clButton = (Button) findViewById(R.id.cl);
clButton.setText("1");
} else if (random0to1 <= 2 / 3) { // As same as in a previous branch
nl = 1;
Button nlButton = (Button) findViewById(R.id.nl);
nlButton.setText("1");
} else if (random0to1 <= 1) {
sl = 1;
Button slButton = (Button) findViewById(R.id.sl);
slButton.setText("1");
}
}