Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.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_Android - Fatal编程技术网

Java 多项选择题测验不承认答案

Java 多项选择题测验不承认答案,java,android,Java,Android,每当我运行应用程序时,它都会给我一个祝酒词,说“正确”,或者再试一次,但祝酒词与它应该做的不匹配。 例如,正确的项目显示为“重试”,不正确的项目显示为“正确”。 我尝试了所有不同的变体来修复代码,结果都不正确,或者都被标记为正确。 不知道我错过了什么。 谢谢你的帮助,我是新来的,但我正在努力 public class MainActivity extends AppCompatActivity { private QuestionList mathQuestionList = new Ques

每当我运行应用程序时,它都会给我一个祝酒词,说“正确”,或者再试一次,但祝酒词与它应该做的不匹配。 例如,正确的项目显示为“重试”,不正确的项目显示为“正确”。 我尝试了所有不同的变体来修复代码,结果都不正确,或者都被标记为正确。 不知道我错过了什么。 谢谢你的帮助,我是新来的,但我正在努力

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

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

public static int[] mathCorrectAnswers = new int[]{
       2, 0, 2, 1, 2
};
除此之外,我还想在某些地方更改您的MainActivity代码:

您可以将标签设置到
ImageButton
s。标记可以是任何类型的
对象
。在本例中,我们希望它表示mathOptions的“列”的索引。设置标记只需为每个
ImageButton
执行一次,因此可以在
onCreate()
中一次性完成,而不是在
updateQuestion()
中重复执行。有必要将
ImageButton
变量作为字段,以便可以通过不同的方法访问它们

我想改变的另一件事是你检查答案是否正确的方式。 现在,您可以比较两个
布尔表达式。我想也许你是想把它们连接起来,例如

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

((view.getId() == (R.id.imageView3)) == (tag == theAnswer))
如果两个表达式都是
false
,则第一个表达式将产生
false
,而第二个表达式将产生
true
,因为
false==false
true

但是无论如何都有很多重复的代码,所以我尝试通过在
ondraglister
中引入一个小方法来简化它,该方法将标签(答案索引)与正确答案的索引进行比较

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);
    }
}
public类MainActivity扩展了AppCompatActivity{
公共静态最终字符串TAG=“MyActivity”;
View.OnLongClickListener longClickListener=新视图。OnLongClickListener(){
@凌驾
仅长按公共布尔值(视图v){
ClipData data=ClipData.newPlainText(“,”);
View.DragShadowBuilder myShadowBuilder=newview.DragShadowBuilder(v);
v、 startDrag(数据,myShadowBuilder,v,0);
返回true;
}
};
私有问题列表mathQuestionList=新问题列表();
私有文本视图;
私有图像视图;
私有图像按钮图像选择1;
私有图像按钮图像选择2;
私有图像按钮图像选择3;
私家侦探;
私有内部内核=0;
私人问题编号=0;
//(在onCreate之外)
View.OnDragListener dragListener=新视图.OnDragListener(){
@凌驾
公共布尔onDrag(视图v,DrageEvent事件){
int dragEvent=event.getAction();
开关(dragEvent){
案例DrageEvent.ACTION\u DRAG\u输入:
打破
案例DrageEvent.ACTION\u DRAG\u退出:
打破
案例DrageEvent.ACTION_DROP:
最终视图=(视图)事件。getLocalState();
int id=view.getId();
int标记=(int)view.getTag();
字符串消息=检查应答(标记);
Toast.makeText(MainActivity.this,message,Toast.LENGTH_SHORT).show();
updateQuestion();
}
返回true;
}
专用字符串检查应答(int-answerIndexFromTag){
if(answerIndexFromTag==回答){
Log.v(“MyActivity”,“answer'”+answerIndexFromTag+“'正确”);
核心++;
更新核心(核心);
返回“正确!”;
}
否则{
Log.v(“MyActivity”,“answer'”+answerIndexFromTag+“'不正确”);
返回“再试一次!”;
}
}
};
@凌驾
创建时受保护的void(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.设置longClickListener(longClickListener);
图像选择2.设置longClickListener(longClickListener);
图像选择3.设置longClickListener(longClickListener);
ImageButton imageAnswerSpace=(ImageButton)findViewById(R.id.imageView4);
imageAnswerSpace.setOnDragListener(dragListener);
imageChoice1.setTag(0);
图像选择2.设置标签(1);
图像选择3.设置标签(2);
updateQuestion();
}
私有void updateQuestion(){
Log.d(标记“updateQuestion:question number=“+theQuestionNumber”);
setImageResource(mathQuestionList.getQuestion(问题编号));
设置图像资源(mathQuestionList.getChoice1(问题编号));
设置图像资源(mathQuestionList.getChoice2(问题编号));
设置图像资源(mathQuestionList.getChoice3(问题编号));
答案=数学问题列表。获取正确答案(问题编号);
Log.d(标签“updateQuestion:answer number=“+theAnswer”);
如果(问题编号<4){
问题编号++;
}else if(问题编号==4){
问题编号=0;
}
}
私有void updateScore(int点){
scoreView.setText(“+theScore”);
}
}

我想我也可以调试这个应用程序,所以我把你的代码复制到了Android Studio a
((view.getId() == (R.id.imageView3)) == (tag == theAnswer))
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);
    }
}