我使用随机数生成器和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 :(得分:0)
当您运行guess.nextInt(0)
时,会生成0到您传入的内容之间的随机数,即0(不包括)。要获得1到5之间的随机数,您应该使用guess.nextInt(5) + 1
之类的内容。我认为在这种情况下结果总是5,但我使用调试器或打印出值Logcat进行检查。
答案 1 :(得分:0)
只是一个简单的修改!
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 result= guess.nextInt(6); // this result can be varies from 0 to 5
// 0 out of 5
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);
}
}
}