为什么它调用方法“isCorrect”多次(的onClick),如果我按下按钮只有一次? [英] Why does it call method 'isCorrect' multiple times (onClick) if I hit the button only once?

查看:199
本文介绍了为什么它调用方法“isCorrect”多次(的onClick),如果我按下按钮只有一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做Android的一个简单的问答游戏,现在有只有10题和40的答案。 (4回答每个问题)
有时,当我打一个按钮,它给了我一个以上的正确答案的时间,也可能连做它时,答案是不正确的。
任何想法有什么错在这个code?原因我不是调用isCorrect布尔方法多次(一键)。

code:

 公共类ETBetaActivity扩展活动实现View.OnClickListener {    按钮ANSWER_1,
    ANSWER_2,ANSWER_3,
    ANSWER_4,主;    TextView的q_textview,
    小费;    私人字符串A1,A2,A3,A4 =;    私人INT I1 = 0;
    公共静态INT正确= 0;    私人布尔活着= TRUE;    MediaPlayer的button_click;    私有String [] =问题
   {Q1
    Q2
    Q3
    第四季度,
    Q5,// 5
    Q6
    Q7
    Q8
    Q9
    Q10// 10
    };
    公共静态INT question_amount = 10;
    私有String [] = answers_correct
   {正确答案 - 1,
    正确答案 - 2,
    正确答案 - 3
    正确答案 - 4,
    正确答案 - 5,
    正确答案 - 6,
    正确答案 - 7
    正确答案 - 8
    正确答案 - 9
    正确答案 - 10
    };    私有String [] [] = answers_wrong
    {{Q1-1,Q1-2,Q1-3},
      {Q2-1,Q2-2,Q2-3},
      {Q3-1,Q3-2,Q3-3},
      {Q4-1,Q4-2,Q4-3},
      {Q5-1,Q5-2,Q5-3},
      {Q6-1,Q6-2,Q6-3},
      {Q7-1,Q7-2,Q7-3},
      {Q8-1,Q8-2,Q8-3},
      {Q9-1,Q9-2,Q9-3},
      {Q10-1,Q10-2,Q10-3}    };
    清单<串GT; question_list =新的ArrayList<串GT;();
    清单<串GT; answer_list_correct =新的ArrayList<串GT;();
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        的getData();
        游戏(I1);
    }
    @覆盖
    公共无效的onClick(查看视图){
        如果(活着== FALSE){
        // startActivity(新意图(com.aleksei.etb.END));
            返回;
        }
        button_click = MediaPlayer.create(这一点,R.raw.button_click);
        button_click.start();
        开关(view.getId()){
        案例R.id.button5://主
            打破;
        案例R.id.button1:// ANSWER_1
            如果(isCorrect(1))
                正确++;
            打破;
        案例R.id.button2:// ANSWER_2
            如果(isCorrect(2))
                正确++;            打破;
        案例R.id.button3:// ANSWER_3
            如果(isCorrect(3))
                正确++;            打破;
        案例R.id.button4:// ANSWER_3
            如果(isCorrect(4))
                正确++;
            打破;        默认:
            打破;
        }
        游戏(I1);
        tip.setText(正确的答案:+正确的);
    }
    公共静态INT getResults(){
        int值=(INT)Math.floor((*正确5)/ question_amount);
        如果(价值< = 0)
        返回1;
        其他
        返回值;
    }    私人布尔isCorrect(INT按钮){
        的for(int i = 0; I< answers_correct.length;我++){
        如果(按钮== 1和;&放大器; A1 == answers_correct [Ⅰ]
            ||按钮== 2和;&放大器; A2 == answers_correct [I]
            ||按钮== 3和;&放大器; A3 == answers_correct [I]
            ||按钮== 4和&放大器; A4 == answers_correct [I])
            返回true;
        }
        返回false;
    }    私人无效游戏(INT Q){
        如果(I1 == question_amount){//没有问题
            startActivity(新意图(com.aleksei.etb.END));
            活着= FALSE;
            返回;
        }
        尝试{
        main.setText(不知道);
        字符串answer_list [] = {
        answers_correct [Q],answers_wrong [Q] [0],answers_wrong [问] [1],answers_wrong [问] [2]
        };        Collections.shuffle(Arrays.asList(answer_list));
        answer_1.setText(answer_list [0]);
        answer_2.setText(answer_list [1]);
        answer_3.setText(answer_list [2]);
        answer_4.setText(answer_list [3]);
        A1 = answer_list [0];
        A2 = answer_list [1];
        A3 = answer_list [2];
        A4 = answer_list [3];
        q_textview.setText(问题[Q]);
        }赶上(例外前){}
        I1 ++;
    }
    私人无效的getData(){
        //获取数据
        主要=(按钮)findViewById(R.id.button5);
        ANSWER_1 =(按钮)findViewById(R.id.button1);
        ANSWER_2 =(按钮)findViewById(R.id.button2);
        ANSWER_3 =(按钮)findViewById(R.id.button3);
        ANSWER_4 =(按钮)findViewById(R.id.button4);
        q_textview =(的TextView)findViewById(R.id.question);
        尖端=(的TextView)findViewById(R.id.answ1);        //使得按键,实际工作
        main.setOnClickListener(本);
        answer_1.setOnClickListener(本);
        answer_2.setOnClickListener(本);
        answer_3.setOnClickListener(本);
        answer_4.setOnClickListener(本);        //重新设置文本
        //提醒自己:更换另一ContectView
        main.setText(开始!);
        answer_4.setText();
        answer_3.setText();
        answer_2.setText();
        answer_1.setText();
        tip.setText();
    }    }


解决方案

考虑下面的变化,看看它是否提高了:

 私人INT questionNumber = -1; 私人无效游戏(INT Q){
    ...
    questionNumber = Q;
    q_textview.setText(问题[Q]);
    ...
 }私人布尔isCorrect(INT按钮){
    如果(按钮== 1和;&放大器; a1.equals(answers_correct [questionNumber])
        ||按钮== 2和;&放大器; a2.equals(answers_correct [questionNumber])
        ||按钮== 3和;&放大器; a3.equals(answers_correct [questionNumber])
        ||按钮== 4和&放大器; a4.equals(answers_correct [questionNumber]))
        返回true;
    }
    返回false;
}

存放问题编号为现场消除了循环遍历所有的问题。
你应该总是使用 String.equals 而不是 ==

I made a simple quiz game for android, right now there's only 10 questions, and 40 answers. (4 answers for each question) Sometimes when I hit a button it gives me more than one correct answer at a time, also it might even do it when answer's not correct. Any idea what's wrong in this code? Cause Im not calling the isCorrect boolean method multiple times (in one click).

Code:

public class ETBetaActivity extends Activity implements View.OnClickListener {

    Button answer_1,
    answer_2,answer_3,
    answer_4,main;

    TextView q_textview,
    tip;

    private String a1,a2,a3,a4 = "";

    private int i1 = 0;
    public static int correct = 0;

    private boolean alive = true;

    MediaPlayer button_click;

    private String[] questions =
   {"Q1", 
    "Q2",
    "Q3", 
    "Q4",
    "Q5", //5
    "Q6", 
    "Q7", 
    "Q8", 
    "Q9",
    "Q10" //10
    };
    public static int question_amount = 10;
    private String[] answers_correct =
   {"Correct answer - 1",
    "Correct answer - 2",
    "Correct answer - 3", 
    "Correct answer - 4",
    "Correct answer - 5",
    "Correct answer - 6",
    "Correct answer - 7",
    "Correct answer - 8",
    "Correct answer - 9",
    "Correct answer - 10"
    };

    private String[][] answers_wrong = 
    { {"Q1-1", "Q1-2" , "Q1-3"},
      {"Q2-1", "Q2-2" , "Q2-3"},
      {"Q3-1", "Q3-2" , "Q3-3"},
      {"Q4-1", "Q4-2" , "Q4-3"},
      {"Q5-1", "Q5-2" , "Q5-3"},
      {"Q6-1", "Q6-2" , "Q6-3"},
      {"Q7-1", "Q7-2" , "Q7-3"},
      {"Q8-1", "Q8-2" , "Q8-3"},
      {"Q9-1", "Q9-2" , "Q9-3"},
      {"Q10-1", "Q10-2" , "Q10-3"}

    };


    List<String> question_list = new ArrayList<String>();
    List<String> answer_list_correct = new ArrayList<String>();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getData();
        Game(i1);
    }


    @Override
    public void onClick(View view) {
        if (alive == false) {
        //  startActivity(new Intent("com.aleksei.etb.END"));
            return;
        }
        button_click = MediaPlayer.create(this, R.raw.button_click);
        button_click.start();
        switch(view.getId()){
        case R.id.button5: //main
            break;
        case R.id.button1: //answer_1
            if(isCorrect(1))
                correct++;


            break;
        case R.id.button2: //answer_2
            if(isCorrect(2))
                correct++;

            break;
        case R.id.button3: //answer_3
            if(isCorrect(3))
                correct++;

            break;
        case R.id.button4: //answer_3
            if(isCorrect(4))
                correct++;


            break;

        default:
            break;


        }
        Game(i1);
        tip.setText("Correct answers: "+correct);
    }


    public static int getResults(){
        int value = (int) Math.floor((correct*5)/question_amount);
        if(value <= 0)
        return 1;
        else
        return value;
    }

    private boolean isCorrect(int button){
        for (int i = 0; i < answers_correct.length; i++){
        if(button == 1 && a1 == answers_correct[i]
            || button == 2 && a2 == answers_correct[i]
            || button == 3 && a3 == answers_correct[i]
            || button == 4 && a4 == answers_correct[i])
            return true;
        }
        return false; 
    }

    private void Game(int q){
        if(i1 == question_amount) { //no more questions
            startActivity(new Intent("com.aleksei.etb.END"));
            alive = false;
            return;
        }
        try {
        main.setText("Dunno");
        String answer_list[] = {
        answers_correct[q], answers_wrong[q][0] , answers_wrong[q][1] , answers_wrong[q][2]     
        };

        Collections.shuffle(Arrays.asList(answer_list));
        answer_1.setText(answer_list[0]);
        answer_2.setText(answer_list[1]);
        answer_3.setText(answer_list[2]);
        answer_4.setText(answer_list[3]);
        a1 = answer_list[0];
        a2 = answer_list[1];
        a3 = answer_list[2];
        a4 = answer_list[3];
        q_textview.setText(questions[q]);
        } catch (Exception ex){}
        i1++;
    }
    private void getData(){
        //Getting the data
        main = (Button) findViewById(R.id.button5);
        answer_1 = (Button) findViewById(R.id.button1);
        answer_2 = (Button) findViewById(R.id.button2);
        answer_3 = (Button) findViewById(R.id.button3);
        answer_4 = (Button) findViewById(R.id.button4);
        q_textview = (TextView) findViewById(R.id.question);
        tip = (TextView) findViewById(R.id.answ1);

        //Making the buttons, actually work
        main.setOnClickListener(this);
        answer_1.setOnClickListener(this);
        answer_2.setOnClickListener(this);
        answer_3.setOnClickListener(this);
        answer_4.setOnClickListener(this);

        //Resets the text
        //Note to self: Replace with another ContectView
        main.setText("Begin!");
        answer_4.setText("");
        answer_3.setText("");
        answer_2.setText("");
        answer_1.setText("");
        tip.setText("");
    }

    }

解决方案

Consider the following changes and see if it improves:

 private int questionNumber = -1;

 private void Game(int q){
    ...
    questionNumber = q;
    q_textview.setText(questions[q]);
    ...
 }

private boolean isCorrect(int button){
    if(button == 1 && a1.equals(answers_correct[questionNumber])
        || button == 2 && a2.equals(answers_correct[questionNumber])
        || button == 3 && a3.equals(answers_correct[questionNumber])
        || button == 4 && a4.equals(answers_correct[questionNumber]))
        return true;
    }
    return false; 
}

Storing the question number as a field eliminates the for loop over all the questions. You should always use String.equals instead of ==.

这篇关于为什么它调用方法“isCorrect”多次(的onClick),如果我按下按钮只有一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆