Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.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 如何修复随机数组中的此错误_Java_Arrays_Android Studio - Fatal编程技术网

Java 如何修复随机数组中的此错误

Java 如何修复随机数组中的此错误,java,arrays,android-studio,Java,Arrays,Android Studio,我目前正在开发这个简单的应用程序,创建一个包含十个问题的测验,并将其随机设置。这是我到目前为止的进展,问题是随机的,这是我需要的,但问题是这些问题是重复的,我只需要它们显示一次。请帮我修复这个错误 activity_quick.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/ap

我目前正在开发这个简单的应用程序,创建一个包含十个问题的测验,并将其随机设置。这是我到目前为止的进展,问题是随机的,这是我需要的,但问题是这些问题是重复的,我只需要它们显示一次。请帮我修复这个错误

activity_quick.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"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:layout_marginBottom="40dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Score"
            android:textSize="20sp"
            android:layout_alignParentLeft="true"
            android:id="@+id/score_text"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/score"
            android:layout_alignParentRight="true"
            android:text="0"
            android:textSize="20sp"/>

    </RelativeLayout>

    <TextView
        android:id="@+id/question"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:padding="80dp"
        android:text="Question"
        android:textSize="15sp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Choice 1"
        android:padding="8dp"
        android:layout_marginBottom="24dp"
        android:id="@+id/choice1" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Choice 2"
        android:padding="8dp"
        android:layout_marginBottom="24dp"
        android:id="@+id/choice2" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Choice 3"
        android:padding="8dp"
        android:layout_marginBottom="24dp"
        android:id="@+id/choice3" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Choice 4"
        android:padding="8dp"
        android:layout_marginBottom="24dp"
        android:id="@+id/choice4" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/backQuiz"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="100dp"
            android:text="Back to Table of Content"
            />
    </RelativeLayout>


</LinearLayout>```
QuizActivity.java:

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Random;

public class QuizActivity extends AppCompatActivity {


    TextView mScoreView,mQuestionView;
    Button mButtonChoice1,mButtonChoice2,mButtonChoice3,mButtonChoice4,backQuiz;

    private QuestionsLibrary mQuestions = new QuestionsLibrary();

    private String mAnswer;
    private int mScore = 0;
    private int num = 0;
    private int mQuestionsLenght = mQuestions.mQuestions.length;

    Random r;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_quiz);

        r = new Random ();

        mScoreView = (TextView) findViewById (R.id.score);
        mQuestionView = (TextView) findViewById (R.id.question);
        mButtonChoice1 = (Button) findViewById (R.id.choice1);
        mButtonChoice2 = (Button) findViewById (R.id.choice2);
        mButtonChoice3 = (Button) findViewById (R.id.choice3);
        mButtonChoice4 = (Button) findViewById (R.id.choice4);
        backQuiz = (Button) findViewById (R.id.backQuiz);

        mScoreView.setText ("Score:" + mScore);

        updateQuestion (1+r.nextInt(mQuestionsLenght));

        mButtonChoice1.setOnClickListener (new View.OnClickListener (){
            @Override
            public void onClick(View v) {

                if (mButtonChoice1.getText () == mAnswer){
                    mScore = mScore + 1;
                    mScoreView.setText ("Score:" + mScore);
                    updateScore(mScore);
                    updateQuestion (r.nextInt (mQuestionsLenght));

                    Toast.makeText (QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show ();
                }else {
                    Toast.makeText (QuizActivity.this, "Wrong", Toast.LENGTH_SHORT).show ();
                    updateQuestion (r.nextInt (mQuestionsLenght));
                }
            }
        });

        mButtonChoice2.setOnClickListener (new View.OnClickListener (){
            @Override
            public void onClick(View v) {

                if (mButtonChoice2.getText () == mAnswer){
                    mScore = mScore + 1;
                    mScoreView.setText ("Score:" + mScore);
                    updateScore(mScore);
                    updateQuestion (r.nextInt (mQuestionsLenght));

                    Toast.makeText (QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show ();
                }else {
                    Toast.makeText (QuizActivity.this, "Wrong", Toast.LENGTH_SHORT).show ();
                    updateQuestion (r.nextInt (mQuestionsLenght));
                }
            }
        });

        mButtonChoice3.setOnClickListener (new View.OnClickListener (){
            @Override
            public void onClick(View v) {

                if (mButtonChoice3.getText () == mAnswer){
                    mScore = mScore + 1;
                    mScoreView.setText ("Score:" + mScore);
                    updateScore(mScore);
                    updateQuestion (r.nextInt (mQuestionsLenght));

                    Toast.makeText (QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show ();
                }else {
                    Toast.makeText (QuizActivity.this, "Wrong", Toast.LENGTH_SHORT).show ();
                    updateQuestion (r.nextInt (mQuestionsLenght));
                }
            }
        });

        mButtonChoice4.setOnClickListener (new View.OnClickListener (){
            @Override
            public void onClick(View v) {

                if (mButtonChoice4.getText () == mAnswer){
                    mScore = mScore + 1;
                    mScoreView.setText ("Score:" + mScore);
                    updateScore(mScore);
                    updateQuestion (r.nextInt (mQuestionsLenght));

                    Toast.makeText (QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show ();
                }else {
                    Toast.makeText (QuizActivity.this, "Wrong", Toast.LENGTH_SHORT).show ();
                    updateQuestion (r.nextInt (mQuestionsLenght));
                }
            }
        });

        backQuiz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(QuizActivity.this, tableofcontentActivity2.class);
                startActivity(i);
            }
        });


    }

    private void updateQuestion(int num){
        mQuestionView.setText (mQuestions.getQuestion (num));
        mButtonChoice1.setText (mQuestions.getChoice1 (num));
        mButtonChoice2.setText (mQuestions.getChoice2 (num));
        mButtonChoice3.setText (mQuestions.getChoice3 (num));
        mButtonChoice4.setText (mQuestions.getChoice4 (num));

        mAnswer = mQuestions.getCorrectAnswer (num);
    }
    private void updateScore(int point) {
        mScoreView.setText ("" + mScore);
    }
急切初始化
  • 创建一个混乱的问题列表
  • 根据无序顺序反复使用问题
  • 洗牌
    List shuffledQuestions=IntStream.range(0,mQuestionsLenght)
    .boxed().collect(collector.toList());
    集合。洗牌(洗牌问题);
    
    用迭代索引替换random.nextInt
  • 使用
    currentIndex=0
  • 使用
    mQuestions.getQuestion(currentIndex++)而不是
    r.nextInt(mquestionLength)
  • 惰性失效
  • 维护一组
    seen
    问题
  • 重复生成
    r.nextInt
    ,如果它已在
    中查看
  • 使用生成的问号作为
    查看
    (添加到集合)
  • import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import java.util.Random;
    
    public class QuizActivity extends AppCompatActivity {
    
    
        TextView mScoreView,mQuestionView;
        Button mButtonChoice1,mButtonChoice2,mButtonChoice3,mButtonChoice4,backQuiz;
    
        private QuestionsLibrary mQuestions = new QuestionsLibrary();
    
        private String mAnswer;
        private int mScore = 0;
        private int num = 0;
        private int mQuestionsLenght = mQuestions.mQuestions.length;
    
        Random r;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate (savedInstanceState);
            setContentView (R.layout.activity_quiz);
    
            r = new Random ();
    
            mScoreView = (TextView) findViewById (R.id.score);
            mQuestionView = (TextView) findViewById (R.id.question);
            mButtonChoice1 = (Button) findViewById (R.id.choice1);
            mButtonChoice2 = (Button) findViewById (R.id.choice2);
            mButtonChoice3 = (Button) findViewById (R.id.choice3);
            mButtonChoice4 = (Button) findViewById (R.id.choice4);
            backQuiz = (Button) findViewById (R.id.backQuiz);
    
            mScoreView.setText ("Score:" + mScore);
    
            updateQuestion (1+r.nextInt(mQuestionsLenght));
    
            mButtonChoice1.setOnClickListener (new View.OnClickListener (){
                @Override
                public void onClick(View v) {
    
                    if (mButtonChoice1.getText () == mAnswer){
                        mScore = mScore + 1;
                        mScoreView.setText ("Score:" + mScore);
                        updateScore(mScore);
                        updateQuestion (r.nextInt (mQuestionsLenght));
    
                        Toast.makeText (QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show ();
                    }else {
                        Toast.makeText (QuizActivity.this, "Wrong", Toast.LENGTH_SHORT).show ();
                        updateQuestion (r.nextInt (mQuestionsLenght));
                    }
                }
            });
    
            mButtonChoice2.setOnClickListener (new View.OnClickListener (){
                @Override
                public void onClick(View v) {
    
                    if (mButtonChoice2.getText () == mAnswer){
                        mScore = mScore + 1;
                        mScoreView.setText ("Score:" + mScore);
                        updateScore(mScore);
                        updateQuestion (r.nextInt (mQuestionsLenght));
    
                        Toast.makeText (QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show ();
                    }else {
                        Toast.makeText (QuizActivity.this, "Wrong", Toast.LENGTH_SHORT).show ();
                        updateQuestion (r.nextInt (mQuestionsLenght));
                    }
                }
            });
    
            mButtonChoice3.setOnClickListener (new View.OnClickListener (){
                @Override
                public void onClick(View v) {
    
                    if (mButtonChoice3.getText () == mAnswer){
                        mScore = mScore + 1;
                        mScoreView.setText ("Score:" + mScore);
                        updateScore(mScore);
                        updateQuestion (r.nextInt (mQuestionsLenght));
    
                        Toast.makeText (QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show ();
                    }else {
                        Toast.makeText (QuizActivity.this, "Wrong", Toast.LENGTH_SHORT).show ();
                        updateQuestion (r.nextInt (mQuestionsLenght));
                    }
                }
            });
    
            mButtonChoice4.setOnClickListener (new View.OnClickListener (){
                @Override
                public void onClick(View v) {
    
                    if (mButtonChoice4.getText () == mAnswer){
                        mScore = mScore + 1;
                        mScoreView.setText ("Score:" + mScore);
                        updateScore(mScore);
                        updateQuestion (r.nextInt (mQuestionsLenght));
    
                        Toast.makeText (QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show ();
                    }else {
                        Toast.makeText (QuizActivity.this, "Wrong", Toast.LENGTH_SHORT).show ();
                        updateQuestion (r.nextInt (mQuestionsLenght));
                    }
                }
            });
    
            backQuiz.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(QuizActivity.this, tableofcontentActivity2.class);
                    startActivity(i);
                }
            });
    
    
        }
    
        private void updateQuestion(int num){
            mQuestionView.setText (mQuestions.getQuestion (num));
            mButtonChoice1.setText (mQuestions.getChoice1 (num));
            mButtonChoice2.setText (mQuestions.getChoice2 (num));
            mButtonChoice3.setText (mQuestions.getChoice3 (num));
            mButtonChoice4.setText (mQuestions.getChoice4 (num));
    
            mAnswer = mQuestions.getCorrectAnswer (num);
        }
        private void updateScore(int point) {
            mScoreView.setText ("" + mScore);
        }