【问题标题】:Random number generator not printing numbers correctly随机数生成器无法正确打印数字
【发布时间】:2015-07-06 07:53:54
【问题描述】:

我的 JavaScript 程序应该打印出用户给定的两个最大值和最小值之间的随机数。它适用于预先输入的数字,但只要我介绍获取最大值和最小值的提示,数字就会开始有趣地打印出来。

var min, max, hmany, i;
var n, w, ans, rsp;


min = prompt("Minimum number for addition problems");
//console.log(min);
max = prompt("Maximum number for addition problems");
//console.log(max);
hmany = prompt("How many questions do you want to answer?");

 for (i = 0; i < 10; i++)
   {
     n = Math.floor(Math.random() * (max - min + 1)) + min;
     console.log(n);
   } 

样本输出:(最小值为 1,最大值为 10)

21
61
71
51
41
01
01
11
21
11

【问题讨论】:

    标签: javascript


    【解决方案1】:

    现在,您正在对字符串进行操作。当最小值为“1”时,你要做的是

    n = someOperation + "1",
    

    给出一个以"1"结尾的字符串。

    您必须将字符串解析为数字:

    min = parseFloat(prompt("Minimum number for addition problems"));
    

    【讨论】:

    • @AustinDonley 您不接受答案有什么原因吗?缺少什么?
    • 只是忘记了。直到当时它已经 15 分钟大了。并忘记了。效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 2014-07-02
    相关资源
    最近更新 更多