【问题标题】:How to randomly generate random operators in android studio如何在android studio中随机生成随机运算符
【发布时间】:2021-09-12 11:38:34
【问题描述】:

公共类 MainActivity 扩展 AppCompatActivity {

Button startButton;
ArrayList<Integer> answers=new ArrayList<Integer>();
int LocationOfRightAnswer;
TextView resultTextView;
TextView pointText;
Button button1;
Button button2;
Button button3;
Button button4;
int score=0;
int numberofquestions = 0;
TextView sumText;
TextView timertext;
RelativeLayout game;
Button playAgain;



public void playAgain(View view) {

    score = 0;
    numberofquestions = 0 ;
    timertext.setText("30s");
    pointText.setText("0/0");
    resultTextView.setText("");
    playAgain.setVisibility(View.INVISIBLE);

    generateQuestion();

    new CountDownTimer(30100,1000) {

        @Override
        public void onTick(long millisUntilFinished) {

            timertext.setText(String.valueOf(millisUntilFinished / 1000) + "s");

        }

        @Override
        public void onFinish() {

            playAgain.setVisibility(View.VISIBLE);
            timertext.setText("0s");
            resultTextView.setText("Your Score is " + Integer.toString(score) + "/" + Integer.toString(numberofquestions));

        }
    }.start();


}


public void generateQuestion() {
    Random random = new Random();

    int a = random.nextInt(21);
    int b = random.nextInt(21);

    char[] ops = {'+' , '-' ,'*', '/'};
    int l = random.nextInt(3) ;


    sumText.setText(Integer.toString(a) + "+"  + Integer.toString(b) );

    LocationOfRightAnswer = random.nextInt(4);
    answers.clear();

    int incorrectAnswer;

    for (int i=0; i<4; i++) {

        if (i == LocationOfRightAnswer) {

            answers.add(a + b);

        }else {

            incorrectAnswer = random.nextInt(41);

            while (incorrectAnswer == a + b) {
                incorrectAnswer = random.nextInt(41);
            }
            answers.add(incorrectAnswer);
        }
    }


    button1.setText(Integer.toString(answers.get(0)));
    button2.setText(Integer.toString(answers.get(1)));
    button3.setText(Integer.toString(answers.get(2)));
    button4.setText(Integer.toString(answers.get(3)));


}



public void chooseAnswer(View view) {

    if (view.getTag().toString().equals(Integer.toString(LocationOfRightAnswer))) {

        score++;
        resultTextView.setText("Correct");

    } else {
        resultTextView.setText("Wrong!");

    }

    numberofquestions++;
    pointText.setText(Integer.toString(score) + "/" + Integer.toString(numberofquestions));
    generateQuestion();

}

public void start (View view) {

    startButton.setVisibility(View.INVISIBLE);
    game.setVisibility(View.VISIBLE);


}

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

    startButton =(Button) findViewById(R.id.startButton);
   sumText = (TextView) findViewById(R.id.sumTextView);
  button1 = (Button) findViewById(R.id.button1);
     button2 = (Button) findViewById(R.id.button2);
     button3 = (Button) findViewById(R.id.button3);
     button4 = (Button) findViewById(R.id.button4);
    resultTextView = (TextView) findViewById(R.id.resultTextView);
    pointText = (TextView) findViewById(R.id.pointsTextView);
    timertext = (TextView) findViewById(R.id.timerTextView);
    playAgain = (Button) findViewById(R.id.playAgain);
    game = (RelativeLayout) findViewById(R.id.gamelayout) ;




playAgain(findViewById(R.id.playAgain));



}

}

请帮助我调整我的代码,以便我的应用程序可以使用所有运算符,而不仅仅是加法。我试过这样做,但我只设法为运算符创建数组列表,但我找不到更改代码以包含所有运算符的方法。

【问题讨论】:

    标签: java android arrays operators


    【解决方案1】:

    试试这个:

    ArrayList<Double> answers=new ArrayList<Double>();
    
    
    
    
    public void generateQuestion() {
        Random random = new Random();
    
        int a = random.nextInt(21);
        int b = random.nextInt(21);
    
        char[] ops = {'+' , '-' ,'*', '/'};
        int l = random.nextInt(3) ;
        
        char op = ops[random.nextInt(4)];
        double correctAns=0;
        
        switch(op){
            case '+' : correctAns = a+b;
               break;
            case '/' : correctAns = (double)a/b;
               break;
            case '*' : correctAns = a*b;
               break;
            case '-' : correctAns = a-b;
               break;
          }
    
    
        sumText.setText(Integer.toString(a) + op  + Integer.toString(b) );
    
        LocationOfRightAnswer = random.nextInt(4);
        answers.clear();
    
        int incorrectAnswer;
    
        for (int i=0; i<4; i++) {
    
            if (i == LocationOfRightAnswer) {
    
                answers.add(correctAns);
    
            }else {
    
                incorrectAnswer = random.nextInt(41);
    
                while (incorrectAnswer != correctAns) {
                    incorrectAnswer = random.nextInt(41);
                }
                answers.add((double)incorrectAnswer);
            }
        }
    
    
        button1.setText(Double.toString(answers.get(0)));
        button2.setText(Double.toString(answers.get(1)));
        button3.setText(Double.toString(answers.get(2)));
        button4.setText(Double.toString(answers.get(3)));
    
    
    }
    
    1. ArrayList&lt;Integer&gt; answers=new ArrayList&lt;Integer&gt;(); 更改为ArrayList&lt;Double&gt; answers=new ArrayList&lt;Double&gt;();int/int 可以是double
    2. 创建一个包含正确答案的double 变量(int/int 可以是double
    3. 随机选择一个操作。
    4. 使用switch 定义在ab 上需要哪些操作,以防选择每个操作。
    5. 将文本设置为Integer.toString(a) + op + Integer.toString(b) 而不是Integer.toString(a) + "+" + Integer.toString(b)
    6. a+b 交换为correctAns

    【讨论】:

    • 答案.add(correctAns);不工作它说它应该被初始化
    • ArrayList&lt;Double&gt; answers=new ArrayList&lt;Double&gt;(); 用这个代替ArrayList&lt;Integer&gt; answers=new ArrayList&lt;Integer&gt;();
    • 它告诉我将 'default' 分支添加到初始化 'correctAns' 的 'switch' 语句中
    • @ZuhayrAlarakha 哦。没有必要,因为无论如何都会定义correctAns 。反正我改了。只需将 correctAns 初始化为 0 查看更新
    猜你喜欢
    • 1970-01-01
    • 2022-10-07
    • 2019-07-03
    • 2018-11-25
    • 2018-06-04
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    相关资源
    最近更新 更多