利用随机运营商 [英] Using random operators

查看:123
本文介绍了利用随机运营商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个机器人的数学游戏,给用户带来了一些问题与随机数来回答,只是为了完善我想尝试使用随机操作,即添加/分/多/分

I'm making an android maths game which gives users the some questions to answer with random numbers, and just to make it perfect I want to try and use random operators i.e. add/sub/multi/divide

到目前为止,我有半随机方程法:

So far I have the semi-random equation method:

public void easyGame(){
    Random rand = new Random();
    final int a = (int) rand.nextInt(50)+1;
    final int b = (int) rand.nextInt(50)+1;
    final int c = (int) rand.nextInt(10)+1;
    String aString = Integer.toString(a);
    String bString = Integer.toString(b);
    String cString = Integer.toString(c);
    String display = aString + " + " + bString + " - " + cString + " =";
    questionLabel.setText(display);
    c1 = a + b - c;
}

这给我的随机数,存储在C1这是一个静态INT,并与在code用户输入以后进行比较。

This gives me random numbers, stored in c1 which is a static int, and compared with user input later on in the code.

我已经看到了如何随机运营商正在使用数组和东西做的,但我不知道/了解如何在 C1 = A + B使用它们 - ℃; 语句,也就是你怎么用+和更换 - 操作

I've seen how random operators are made using arrays and stuff, but I don't know/understand how to use them in the c1 = a + b - c; statement, i.e. how do you replace it with the + and - operations.

请让我知道如果你知道的。谢谢

Please let me know if you know. Thank you

推荐答案

您可以使用随机数做你想要跟他们任何东西。例如,这(非常不完整)的例子应该给你一个想法如何可以随机运营商了。

You can use random numbers to do anything you want with them. For example this (very incomplete) example should give you an idea how you can randomize the operators too.

int operator1 =  (int) rand.nextInt(4);
// ...
char op;
switch(operator1) {
   case 0: op = '+'; break;
   case 1: op = '-'; break;
   // ...
}
String display = aString + op + ...
c1 = a;
switch(operator1) {
   case 0: c1 += b; break;
   case 1: c1 -= b; break;
   // ...
 }

这篇关于利用随机运营商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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