如何在android studio中随机生成随机运算符
Posted
技术标签:
【中文标题】如何在android studio中随机生成随机运算符【英文标题】:How to randomly generate random operators in 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));
请帮助我调整我的代码,以便我的应用程序可以使用所有运算符,而不仅仅是加法。我试过这样做,但我只设法为运算符创建数组列表,但我找不到更改代码以包含所有运算符的方法。
【问题讨论】:
【参考方案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)));
-
将
ArrayList<Integer> answers=new ArrayList<Integer>();
更改为ArrayList<Double> answers=new ArrayList<Double>();
(int/int
可以是double
)
创建一个包含正确答案的double
变量(int/int
可以是double
)
随机选择一个操作。
使用switch
定义在a
和b
上需要哪些操作,以防选择每个操作。
将文本设置为Integer.toString(a) + op + Integer.toString(b)
而不是Integer.toString(a) + "+" + Integer.toString(b)
将a+b
交换为correctAns
【讨论】:
答案.add(correctAns);不工作它说它应该被初始化ArrayList<Double> answers=new ArrayList<Double>();
用这个代替ArrayList<Integer> answers=new ArrayList<Integer>();
它告诉我将 'default' 分支添加到初始化 'correctAns' 的 'switch' 语句中
@ZuhayrAlarakha 哦。没有必要,因为无论如何都会定义correctAns
。反正我改了。只需将 correctAns
初始化为 0 查看更新以上是关于如何在android studio中随机生成随机运算符的主要内容,如果未能解决你的问题,请参考以下文章
如何识别 XCUITest 在 xcode 中运行时抛出的一些随机寡妇
Android Studio Java - 如何使用随机数组而不重复?