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