0
我正在使用随机数生成器和IF语句在活动之间切换。它遍历第一个if语句并停在那里。我不认为我的随机数发生器正在产生任何随机数。提前致谢。使用if语句和随机数生成器切换活动
package app.com.example.android.oraclethedeciscionmaker;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.util.Random;
public class HomeScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
}
public void onClick(View view){
Random guess = new Random();
int guesser0 = guess.nextInt(0) + 1;
int guesser1 = guess.nextInt(0) + 1;
int guesser2 = guess.nextInt(0) + 1;
int guesser3 = guess.nextInt(0) + 1;
int guesser4 = guess.nextInt(0) + 1;
int result = guesser0 + guesser1 + guesser2 + guesser3 + guesser4;
// 0 out of 0
if(result == 0){
Intent intent = new Intent(HomeScreen.this, ZeroOfFive.class);
startActivity(intent);
// If this statement is true go to this activity
}
// 1 out of 5
else if(result == 1){
Intent intent = new Intent(HomeScreen.this, OneOfFive.class);
startActivity(intent);
// If this statement is true go to this activity
}
//2 out of 5
else if(result == 2){
Intent intent = new Intent(HomeScreen.this, TwoOfFive.class);
startActivity(intent);
// If this statement is true go to this activity
}
//3 out of 5
else if(result == 3){
Intent intent = new Intent(HomeScreen.this, ThreeOfFive.class);
startActivity(intent);
// If this statement is true go to this activity
}
//4 out of 5
else if(result == 4){
Intent intent = new Intent(HomeScreen.this, FourOfFive.class);
startActivity(intent);
// If this statement is true go to this activity
}
//5 out of 5
else {
Intent intent = new Intent(HomeScreen.this, FiveOfFive.class);
startActivity(intent);
}
}
}
这意味着结果为0. – GurV
为什么你认为它不起作用? –
guess.nextInt(x)返回一个从0到x的随机数。在你的情况下,它是0到0.所以它总是0,这对你来说是一种常数0。使用诸如guess.nextInt(100)之类的东西,以便它每次生成0到100之间的随机数。或者根据需要使用一些大数字。 – Jimmy