Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.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,我正在编写一个测验应用程序。当我点击“分数”按钮查看它是否有效时,它显示我得到了5分中的0分。我输入了所有正确的答案,但是我的代码没有任何结果。我错过了什么?我不知道还有什么需要补充的,而且我是一个新的程序员,所以我真的可以使用指南。我很感激你能给我的任何帮助 int correctAnswers = 0; // Start score @Override protected void onCreate(Bundle savedInstanceState) { super.onCre

我正在编写一个测验应用程序。当我点击“分数”按钮查看它是否有效时,它显示我得到了5分中的0分。我输入了所有正确的答案,但是我的代码没有任何结果。我错过了什么?我不知道还有什么需要补充的,而且我是一个新的程序员,所以我真的可以使用指南。我很感激你能给我的任何帮助

int correctAnswers = 0;

// Start score

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

public void answers(View view) {

    RadioButton q1 = (RadioButton) findViewById(R.id.yes_radio_button);
    Boolean q1RightAnswer = q1.isChecked();

    if (q1RightAnswer) {
        correctAnswers += 1;
    }
    CheckBox q2Box1 = (CheckBox) findViewById(R.id.box1_checkbox);
    boolean q2Box1RightAnswer = q2Box1.isChecked();

    CheckBox q2Box2 = (CheckBox) findViewById(R.id.box2_checkbox);
    boolean q2Box2WrongAnswer = q2Box2.isChecked();

    CheckBox q2Box3 = (CheckBox) findViewById(R.id.box3_checkbox);
    boolean q2Box3RightAnswer = q2Box3.isChecked();

    if (q2Box1RightAnswer)

        if (q2Box3RightAnswer) {

            correctAnswers += 1;
        }

    if (q2Box2WrongAnswer) {
        correctAnswers += 0;
    }

    RadioButton q3 = (RadioButton) findViewById(R.id.shuri_radio_button);
    Boolean q3RightAnswer = q3.isChecked();

    if (q3RightAnswer) {
        correctAnswers += 1;
    }

    RadioButton q5 = (RadioButton) findViewById(R.id.two_radio_button);
    Boolean q5RightAnswer = q5.isChecked();

    if (q5RightAnswer) {
        correctAnswers += 1;
    }

    EditText q4 = (EditText) findViewById(R.id.wakanda);
    String q4RightAnswer = q4.getText().toString();

    if (q4RightAnswer.equals(correctAnswers)) {
        correctAnswers += 1;
    } else {
        // incorrect, do nothing
    }
}


/**
 * This method is called when the score button is clicked.
 */
public void submitScore(View view) {
    Button nameField = (Button) findViewById(R.id.score);
    String score = nameField.getText().toString();
    // Show score message as a toast
    Toast.makeText(this, "You got " + correctAnswers + "/5 correct!", Toast.LENGTH_LONG).show();
    // Exit this method early because there's nothing left to do
    return;

}

}

这永远不会是真的

q4RightAnswer.equals(correctAnswers)
您需要比较匹配的类型,而不是字符串和整数

假设这就是您要做的,要么解析字符串,要么将int转换为字符串

如果未标记任何复选框或从未调用
answers()
,则将得到零打印。例如,answers方法和SubmitCore方法之间有什么区别?两者都采用一个视图参数,那么哪一个实际指定给单击事件

我建议你做一些类似的事情

RadioButton q1, q3, q5;
EditText q4;
Checkbox qBox1, qBox2;
Button submit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    q1 = (RadioButton) findViewById(R.id.yes_radio_button);
    // assign other views here 
    submit = (Button) findViewById(R.id.score);
    submit.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            int correctAnswers = 0;
            if (q1.isChecked()) correctAnswers += 1;
            // TODO: check other inputs 

            String q4Text = q4.getText().toString();
            if (q4Text.equals(String.valueOf(correctAnswers)) {
                 correctAnswers += 1;
            } 
            // Toast correct answers 
        } 
    });
}

基本上,将所有视图定义为类级变量,然后在内容视图可用后立即设置它们,然后仅在单击按钮时计算分数(换句话说,等待用户输入)。此外,每次单击按钮时重置分数

布尔q3RightAnswer=q3.isChecked();如果(q3RightAnswer){correctAnswers+=1;}RadioButton q5=(RadioButton)findViewById(R.id.two_单选按钮);布尔q5RightAnswer=q5.isChecked();如果(q5RightAnswer){correctAnswers+=1;}EditText q4=(EditText)findViewById(R.id.wakanda);字符串q4RightAnswer=q4.getText().toString();如果(q4RightAnswer.equals(correctAnswers)){correctAnswers+=1;}或者{//不正确,什么都不做}/***单击“分数”按钮时调用此方法。*/public void submitScore(视图){Button nameField=(Button)findViewById(R.id.score);String score=nameField.getText().toString();//将分数消息显示为toast toast.makeText(此“You got”+correctAnswers+“/5 correct!”,toast.LENGTH\u LONG.Show();//尽早退出此方法,因为已无事可做返回;}}请停止对放置代码和您的问题进行注释当我尝试这样做时,它不允许我这样做。但别担心,我没有更多的代码发布!祝你度过愉快的一天!如果这永远不会是真的,那也没关系。我知道这是不对的,这就是为什么我要寻求帮助。虽然我感谢您的回复,但我仍然不知道如何实现您的建议。您不知道如何使用
Integer.parseInt()
?还是别的什么?我添加了更多的代码来演示第一个被计算的问题。不,我不知道如何使用它。我不是你。我已经好几年没有这样做了,我几周前才开始。有一份全职工作和一个家庭,我不能把所有的时间都花在学习编码上。不管怎样我都会解决的,这很公平。我并没有开始通过Android学习Java,我也不会真的推荐它。无论如何,最好的luckI我不知道你在说什么。我想很明显我在努力理解。如果不是,我不会发问。我还认为,很明显,我非常感谢我在帖子中所说的帮助以及帮助我的人。