多项选择测验未确认答案

时间:2018-07-13 16:22:15

标签: java android

每当我运行该应用程序时,它都会给我吐司说正确或重试,但吐司与应有的内容不符。 例如,正确的项目显示了重试的敬酒,而错误的项目显示了正确的敬酒。 我尝试了所有不同的变体来尝试修复代码,然后它们都以不正确的形式出现,或者将它们标记为正确的。 无法弄清楚我所缺少的。 感谢您的帮助,我是新手,但正在尝试。

public class MainActivity extends AppCompatActivity {

private QuestionList mathQuestionList = new QuestionList();
private TextView scoreView;
private Button questionView;
private Button imageAnswerSpace;
private Button imageChoice1;
private Button imageChoice2;
private Button imageChoice3;
private int theAnswer;
private int theScore = 0;
private int theQuestionNumber = 0;


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


    TextView scoreView = (TextView) findViewById(R.id.score);
    ImageButton questionViewButton = (ImageButton) findViewById(R.id.questionView);
    ImageButton imageAnswerSpace = (ImageButton) findViewById(R.id.imageView4);
    final ImageButton imageChoice1 = (ImageButton) findViewById(R.id.imageView1);
    final ImageButton imageChoice2 = (ImageButton) findViewById(R.id.imageView2);
    final ImageButton imageChoice3 = (ImageButton) findViewById(R.id.imageView3);

    imageChoice1.setOnLongClickListener(longClickListener);
    imageChoice2.setOnLongClickListener(longClickListener);
    imageChoice3.setOnLongClickListener(longClickListener);

    imageAnswerSpace.setOnDragListener(dragListener);

    updateQuestion();
}


//(outside of onCreate)

View.OnLongClickListener longClickListener = new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        ClipData data = ClipData.newPlainText("", "");
        View.DragShadowBuilder myShadowBuilder = new View.DragShadowBuilder(v);
        v.startDrag(data, myShadowBuilder, v, 0);

        return true;
    }
};

View.OnDragListener dragListener = new View.OnDragListener() {
    @Override
    public boolean onDrag(View v, DragEvent event) {
        int dragEvent = event.getAction();
        final View view = (View) event.getLocalState();

        switch (dragEvent) {
            case DragEvent.ACTION_DRAG_ENTERED:
                break;
            case DragEvent.ACTION_DRAG_EXITED:
                break;
            case DragEvent.ACTION_DROP:

                //imageview1 clicked and correct
                if ((view.getId() == (R.id.imageView1)) == (v.getId() == theAnswer)) {
                    Log.v("MyActivity", "imageview1 is correct");
                    updateQuestion();
                    Toast.makeText(MainActivity.this, "Correct!", Toast.LENGTH_SHORT).show();


                    //imageview1 clicked and not correct
                } else if ((view.getId() == (R.id.imageView1)) != (view.getId() == theAnswer)) {
                    Log.v("MyActivity", "imageview1 not correct");
                    Toast.makeText(MainActivity.this, "Try Again!", Toast.LENGTH_SHORT).show();
                    updateQuestion();


                    //imageview2 clicked and correct
                } else if ((view.getId() == (R.id.imageView2)) == (v.getId() == theAnswer)) {
                    Log.v("MyActivity", "imageview2 is correct");
                    updateQuestion();
                    Toast.makeText(MainActivity.this, "Correct!", Toast.LENGTH_SHORT).show();


                    //imageview2 clicked and not correct
                } else if ((view.getId() == (R.id.imageView2)) != (view.getId() == theAnswer)) {
                    Log.v("MyActivity", "imageview2 not correct");
                    Toast.makeText(MainActivity.this, "Try Again!", Toast.LENGTH_SHORT).show();
                    updateQuestion();


                    //imageview3 clicked and correct
                } else if ((view.getId() == (R.id.imageView3)) == (v.getId() == theAnswer)) {
                    Log.v("MyActivity", "imageview3 is correct");
                    updateQuestion();
                    Toast.makeText(MainActivity.this, "Correct!", Toast.LENGTH_SHORT).show();


                    //imageview3 clicked and not correct
                } else if ((view.getId() == (R.id.imageView3)) != (view.getId() == theAnswer)) {
                    Log.v("MyActivity", "imageview3 not correct");
                    Toast.makeText(MainActivity.this, "Try Again!", Toast.LENGTH_SHORT).show();
                    updateQuestion();
                }

        }
        return true;
    }

};



private void updateQuestion() {

    ImageView changeQuestion = findViewById(R.id.questionView);
    changeQuestion.setImageResource(mathQuestionList.getQuestion(theQuestionNumber));

    ImageView changeImage1 = findViewById(R.id.imageView1);
    changeImage1.setImageResource(mathQuestionList.getChoice1(theQuestionNumber));

    ImageView changeImage2 = findViewById(R.id.imageView2);
    changeImage2.setImageResource(mathQuestionList.getChoice2(theQuestionNumber));

    ImageView changeImage3 = findViewById(R.id.imageView3);
    changeImage3.setImageResource(mathQuestionList.getChoice3(theQuestionNumber));


    theAnswer = mathQuestionList.getCorrectAnswer(theQuestionNumber);


    if (theQuestionNumber < 4) {
        theQuestionNumber++;
    } else if (theQuestionNumber == 4) {
        theQuestionNumber = 0;
    }

}


private void updateScore(int point) {
    // scoreView.setText("" + theScore);
}
}

布局:

    <?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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/score"
        android:layout_width="20dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:text="0"
        android:textSize="30sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageButton
            android:id="@+id/questionView"
            android:layout_width="wrap_content"
            android:layout_height="110dp"
            android:layout_weight="2"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter" />

        <ImageButton
            android:id="@+id/imageView4"
            android:layout_width="wrap_content"
            android:layout_height="110dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:src="@drawable/questionmark"
            android:visibility="visible" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="20dp">

        <ImageButton
            android:id="@+id/imageView1"
            android:layout_width="60dp"
            android:layout_height="90dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter" />

        <ImageButton
            android:id="@+id/imageView2"
            android:layout_width="60dp"
            android:layout_height="90dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter" />

        <ImageButton
            android:id="@+id/imageView3"
            android:layout_width="60dp"
            android:layout_height="90dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter" />
    </LinearLayout>

    `enter code here`</LinearLayout>

答案

public class QuestionList {

public static int[] mathQuestions = new int[]{
        R.drawable.image35,
        R.drawable.image23,
        R.drawable.image44,
        R.drawable.image15,
        R.drawable.image26
};

public static int mathOptions [][] = {
        {R.drawable.number4, R.drawable.number2, R.drawable.number8},
        {R.drawable.number5, R.drawable.number1, R.drawable.number7},
        {R.drawable.number4, R.drawable.number6, R.drawable.number8},
        {R.drawable.number7, R.drawable.number6, R.drawable.number3},
        {R.drawable.number9, R.drawable.number5, R.drawable.number8}
};

public static int[] mathCorrectAnswers = new int[]{
        R.drawable.number8,
        R.drawable.number5,
        R.drawable.number8,
        R.drawable.number6,
        R.drawable.number8
};


public int getQuestion(int a) {
    int question = mathQuestions[a];
    return question;
}


public int getChoice1(int a) {
    int choice0 = mathOptions[a][0];
    return choice0;
}


public int getChoice2(int a) {
    int choice1 = mathOptions[a][1];
    return choice1;
}

public int getChoice3(int a) {
    int choice2 = mathOptions[a][2];
    return choice2;
}

public int getCorrectAnswer(int a) {
    int answer = mathCorrectAnswers[a];
    return answer;
}
}

1 个答案:

答案 0 :(得分:0)

我的建议是您不要依靠资源ID来管理测验。您需要从问题到正确答案的映射,如下所示:

public static int[] mathCorrectAnswers = new int[]{
       2, 0, 2, 1, 2
};

除此之外,我想在某些地方更改您的MainActivity代码:

您可以将标签设置为ImageButton。标签可以是任何类型的Object。在这种情况下,我们希望它表示 mathOptions 的“列”的索引。设置标签只需要对每个ImageButton进行一次操作,因此可以一次在onCreate()中进行所有操作,而不必在updateQuestion()中进行重复操作。尽管有必要将ImageButton变量作为字段,以便可以从不同的方法访问它们。

我想更改的另一件事是检查答案是否正确的方式。 现在,您比较两个boolean表达式。我想也许您打算将它们连接起来,例如

((view.getId() == (R.id.imageView3)) && (tag == theAnswer))

代替

((view.getId() == (R.id.imageView3)) == (tag == theAnswer))

如果两个表达式均为false,则第一个将产生false,而第二个表达式将产生true,因为false == falsetrue

但是还是有很多重复的代码,因此我尝试通过在OnDragListener中引入一个小的方法来简化它,该方法会将 tag (答案索引)与索引进行比较正确答案。

public class MainActivity extends AppCompatActivity {
    public static final String TAG = "MyActivity";
    View.OnLongClickListener longClickListener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder myShadowBuilder = new View.DragShadowBuilder(v);
            v.startDrag(data, myShadowBuilder, v, 0);

            return true;
        }
    };
    private QuestionList mathQuestionList = new QuestionList();
    private TextView scoreView;
    private ImageButton questionView;
    private ImageButton imageChoice1;
    private ImageButton imageChoice2;
    private ImageButton imageChoice3;
    private int theAnswer;
    private int theScore = 0;
    private int theQuestionNumber = 0;

    //(outside of onCreate)
    View.OnDragListener dragListener = new View.OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            int dragEvent = event.getAction();

            switch (dragEvent) {
                case DragEvent.ACTION_DRAG_ENTERED:
                    break;
                case DragEvent.ACTION_DRAG_EXITED:
                    break;
                case DragEvent.ACTION_DROP:
                    final View view = (View) event.getLocalState();
                    int id = view.getId();
                    int tag = (int)view.getTag();
                    String message = checkAnswer(tag);
                    Toast.makeText(MainActivity.this, message , Toast.LENGTH_SHORT).show();
                    updateQuestion();
            }
            return true;
        }

        private String checkAnswer(int answerIndexFromTag){
            if (answerIndexFromTag == theAnswer) {
                Log.v("MyActivity", "answer '" + answerIndexFromTag + "' is correct");
                theScore++;
                updateScore(theScore);
                return "Correct!";
            }
            else {
                Log.v("MyActivity", "answer '" + answerIndexFromTag + "' not correct");
                return "Try Again!";
            }
        }
    };

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

        scoreView = (TextView) findViewById(R.id.score);
        questionView = (ImageButton) findViewById(R.id.questionView);

        imageChoice1 = (ImageButton) findViewById(R.id.imageView1);
        imageChoice2 = (ImageButton) findViewById(R.id.imageView2);
        imageChoice3 = (ImageButton) findViewById(R.id.imageView3);

        imageChoice1.setOnLongClickListener(longClickListener);
        imageChoice2.setOnLongClickListener(longClickListener);
        imageChoice3.setOnLongClickListener(longClickListener);

        ImageButton imageAnswerSpace = (ImageButton) findViewById(R.id.imageView4);
        imageAnswerSpace.setOnDragListener(dragListener);

        imageChoice1.setTag(0);
        imageChoice2.setTag(1);
        imageChoice3.setTag(2);
        updateQuestion();

    }

    private void updateQuestion() {
        Log.d(TAG, "updateQuestion: question number = " + theQuestionNumber);

        questionView.setImageResource(mathQuestionList.getQuestion(theQuestionNumber));

        imageChoice1.setImageResource(mathQuestionList.getChoice1(theQuestionNumber));
        imageChoice2.setImageResource(mathQuestionList.getChoice2(theQuestionNumber));
        imageChoice3.setImageResource(mathQuestionList.getChoice3(theQuestionNumber));

        theAnswer = mathQuestionList.getCorrectAnswer(theQuestionNumber);
        Log.d(TAG, "updateQuestion: answer number = " + theAnswer);


        if (theQuestionNumber < 4) {
            theQuestionNumber++;
        } else if (theQuestionNumber == 4) {
            theQuestionNumber = 0;
        }

    }

    private void updateScore(int point) {
        scoreView.setText("" + theScore);
    }
}