Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.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 For循环不等待用户输入_Java_Android_Loops - Fatal编程技术网

Java For循环不等待用户输入

Java For循环不等待用户输入,java,android,loops,Java,Android,Loops,你好,我有一个android项目正在进行中。用户必须回答一个数学问题,然后对答案进行打分/扣分,然后代码应显示第二个不同的问题,直到完成10个问题。问题是代码跳过了循环,并立即完成了游戏。我尝试使用while循环,但同样的问题发生了。我考虑过一个if,但不知道如何重新运行if。下面是项目的全部代码,但主要问题是for循环 package com.example.mixmathsv3; import android.annotation.SuppressLint; import android.c

你好,我有一个android项目正在进行中。用户必须回答一个数学问题,然后对答案进行打分/扣分,然后代码应显示第二个不同的问题,直到完成10个问题。问题是代码跳过了循环,并立即完成了游戏。我尝试使用while循环,但同样的问题发生了。我考虑过一个if,但不知道如何重新运行if。下面是项目的全部代码,但主要问题是for循环

package com.example.mixmathsv3;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;

public class SimpleModeActivity extends AppCompatActivity {

Button checkButt, nextQuestionButt;
TextView resultText, simpleQuestionText, scoreText, questionNumberText;
EditText userAnswer;

public volatile int completedTimes = 0;
public int scoreValue = 0;
public int intUserAnswer = 0;
public String questionAnswer = "";

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_simple_mode);


    resultText = (TextView) findViewById(R.id.result);
    checkButt = (Button) findViewById(R.id.checkButt);
    simpleQuestionText = (TextView) findViewById(R.id.simpleQuestionText);
    scoreText = (TextView) findViewById(R.id.scoreText);
    userAnswer = (EditText) findViewById(R.id.userAnswer);
    //nextQuestionButt = (Button) findViewById(R.id.nextQuestion/Butt);
    //questionNumberText = (TextView) findViewById(R.id.questionNumberText);





    final String[] questionArray = {"5+2", "10+3", "7+1", "9+0", "1+6"};
    final String[] answerArray = {"7", "13", "8", "9", "7"};
    final Random r = new Random();
    int arrayRandom = r.nextInt(questionArray.length);

    simpleQuestionText.setText(questionArray[arrayRandom]);
    questionAnswer = (answerArray[arrayRandom]);



    for (int i = 1; i < 11; i++) {
        //System.out.println(i);
        //Toast.makeText(getApplicationContext(), ("loop successful"), Toast.LENGTH_SHORT).show();



        checkButt.setEnabled(true);


        checkButt.setOnClickListener(new View.OnClickListener() {

            @SuppressLint("SetTextI18n")
            public void onClick(View v) {
                checkButt.setEnabled(false);

                intUserAnswer = Integer.parseInt(userAnswer.getText().toString());
                int intQuestionAnswer = Integer.parseInt(questionAnswer);


                if (intUserAnswer == intQuestionAnswer) {
                    resultText.setText("Correct");
                    scoreValue = scoreValue + 10;
                    //completedTimes = + 1;
                    //questionNumberText.setText(completedTimes);
                    Toast.makeText(getApplicationContext(), ("Your Score is " + scoreValue), Toast.LENGTH_SHORT).show();

                }

                if (intUserAnswer != intQuestionAnswer) {
                    resultText.setText("Incorrect");
                    scoreValue = scoreValue - 5;
                    //completedTimes = + 1;
                    //questionNumberText.setText(completedTimes)
                    Toast.makeText(getApplicationContext(), ("Your Score is " + scoreValue), Toast.LENGTH_SHORT).show();


                }
            }


        });

    }


    Toast.makeText(getApplicationContext(), ("Game Over"), Toast.LENGTH_SHORT).show();
package com.example.mixmathsv3;
导入android.annotation.SuppressLint;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.os.可控震源;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.Toast;
导入androidx.appcompat.app.appcompat活动;
导入java.util.Random;
公共类SimpleModeActivity扩展了AppCompatActivity{
按钮checkButt,nextQuestionButt;
text查看结果text、simpleQuestionText、scoreText、questionNumberText;
编辑文本用户应答;
公共volatile int completedTimes=0;
公共int scoreValue=0;
公共int intUserAnswer=0;
公共字符串questionAnswer=“”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u simple\u模式);
resultText=(TextView)findViewById(R.id.result);
checkButt=(按钮)findViewById(R.id.checkButt);
simpleQuestionText=(TextView)findViewById(R.id.simpleQuestionText);
scoreText=(TextView)findViewById(R.id.scoreText);
userAnswer=(EditText)findViewById(R.id.userAnswer);
//nextQuestionButt=(按钮)findViewById(R.id.nextQuestion/Butt);
//questionNumberText=(TextView)findViewById(R.id.questionNumberText);
最后一个字符串[]questionArray={“5+2”、“10+3”、“7+1”、“9+0”、“1+6”};
最后一个字符串[]answerArray={“7”、“13”、“8”、“9”、“7”};
最终随机r=新随机();
int arrayRandom=r.nextInt(questionArray.length);
setText(questionArray[arrayRandom]);
问题答案=(answerArray[arrayRandom]);
对于(int i=1;i<11;i++){
//系统输出打印LN(i);
//Toast.makeText(getApplicationContext(),(“循环成功”),Toast.LENGTH\u SHORT.show();
checkButt.setEnabled(true);
checkButt.setOnClickListener(新视图.OnClickListener(){
@SuppressLint(“SetTextI18n”)
公共void onClick(视图v){
checkButt.setEnabled(false);
intUserAnswer=Integer.parseInt(userAnswer.getText().toString());
int intQuestionAnswer=Integer.parseInt(questionAnswer);
如果(intUserAnswer==intQuestionAnswer){
resultText.setText(“正确”);
scoreValue=scoreValue+10;
//完成次数=+1;
//问题编号text.setText(完成次数);
Toast.makeText(getApplicationContext(),(“您的分数是”+scoreValue),Toast.LENGTH\u SHORT.show();
}
如果(intUserAnswer!=intQuestionAnswer){
resultText.setText(“不正确”);
scoreValue=scoreValue-5;
//完成次数=+1;
//questionNumberText.setText(完成次数)
Toast.makeText(getApplicationContext(),(“您的分数是”+scoreValue),Toast.LENGTH\u SHORT.show();
}
}
});
}
Toast.makeText(getApplicationContext(),(“游戏结束”),Toast.LENGTH\u SHORT.show();

您不需要将所有内容都放在循环中。将所有逻辑移到循环之外,它就会工作。

setOnClickListener
不会做您认为的事情:
setOnClickListener
用于注册一个方法,该方法只有在用户单击按钮时才会被调用。
在您的示例中,for循环寄存器将一个新的
onClickListener
乘以10次,这不是我们想要的

在您的情况下,您不能使用立即执行的for循环。请在用户单击按钮时更改问题和文本

final String[]questionArray={“5+2”、“10+3”、“7+1”、“9+0”、“1+6”};
最后一个字符串[]answerArray={“7”、“13”、“8”、“9”、“7”};
最终随机r=新随机();
int=0;
//初始问题
int arrayRandom=r.nextInt(questionArray.length);
setText(questionArray[arrayRandom]);
问题答案=(answerArray[arrayRandom]);
checkButt.setEnabled(true);
checkButt.setOnClickListener(新视图.OnClickListener(){
@SuppressLint(“SetTextI18n”)
公共void onClick(视图v){
checkButt.setEnabled(false);
intUserAnswer=Integer.parseInt(userAnswer.getText().toString());
int intQuestionAnswer=Integer.parseInt(questionAnswer);
如果(intUserAnswer==intQuestionAnswer){
resultText.setText(“正确”);
scoreValue=scoreValue+10;
//完成次数=+1;
//问题编号text.setText(完成次数);
Toast.makeText(getApplicationContext(),(“您的分数是”+scoreValue),Toast.LENGTH\u SHORT.show();
}
如果(intUserAnswer!=intQuestionAnswer){
resultText.setText(“不正确”);
scoreValue=scoreValue-5;
//完成次数=+1;
//questionNumberText.setText(完成次数)
Toast.makeText(getApplicationContext(),(“您的分数是”+scoreValue),Toast.LENGTH\u SHORT.show();
}
已回答+++;//我们更新已回答问题的计数
如果(已回答==10){
//完成
Toast.makeText(getApplicationContext(),(“游戏结束”),Toast.LENGTH\u SHORT.show();
}否则{
//变更问题
int arrayRandom=r。