Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用if语句和随机数生成器进行切换活动_Java_Android_If Statement_Android Intent - Fatal编程技术网

Java 使用if语句和随机数生成器进行切换活动

Java 使用if语句和随机数生成器进行切换活动,java,android,if-statement,android-intent,Java,Android,If Statement,Android Intent,我使用随机数生成器和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.V

我使用随机数生成器和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);
        }
    }
}

当您运行
guess.nextInt(0)
时,它会生成一个介于0和您传入的值之间的随机数,即0(独占)。要获得介于1和5之间的随机数,应该使用类似于
guess.nextInt(5)+1的方法。我认为在这种情况下,结果总是5,但我会使用调试器或打印出要检查的值。

只是一个简单的修改

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);
        }
    }
}

这意味着结果是0。为什么你们认为它不起作用?猜猜看。nextInt(x)返回一个从0到x的随机数。在您的情况下,它是0对0。所以它总是0,对你来说是常数。使用像guess.nextInt(100)这样的东西,这样它每次都会生成0到100之间的随机数。或者根据需要使用一些大数字。