当我在这种情况下生成一个问题或数字时,它似乎总是以 0 出现,而它应该生成一个介于 0 和给定值 AD 之间的数字。在这种情况下是 0 到 11,但每次只生成 0。出于某种原因,当我将其记录到调试控制台时,AD int 返回为 1。有帮助吗?
Double d = Math.random() * Max1;
O1 = d.intValue();
Log.d("BasicsPractice.java","the GenerateOperation function generated : " + O1 + " : as the number to determin the operation to be built");
if(O1 == 0) {
if(AddP == true) {
int AD = DM.AddDiff + 1;
Double d1 = Math.random() * AD;
A1 = d1.intValue();
Log.d("BasicsPractice.java","the first Addition number generated was: " + A1);
Double d5 = Math.random() * AD;
A2 = d5.intValue();
Log.d("BasicsPractice.java","the second Addition number generated was: " + A2);
Output = A1 + " + " + A2;
Log.d("BasicsPractice.java","the outputted problem was: " + Output);
OutputToProblemView();
}
最佳答案
Math.random()
返回一个从 0.0 到 1.0 的数字。所以你需要把它转换成int
。
试试这个。
Math.floor(Math.random()*12);
这将生成 0 到 11 之间的数字。
关于android - 生成数字时,只给出 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32731395/