我正在为一门课程编写代码,需要弄清楚为什么代码输出不能正确执行。我对编码非常陌生,这是一个初学者的任务,所以所有的帮助和解释都非常感谢。
输出应如下所示:
Output:
How many times to repeat? 2
Stats Solver execution 1 ====================
give a number 10.10203
give a number 20
give a number 30
give a number 40
give a number 50
sum: 150.10203
average: 30.020406
max: 50
min: 10.10203
ratio: 4.94
Stats Solver execution 2 ====================
give a number 3.21
give a number 2.1
give a number 1
give a number 5.4321
give a number 4.321
sum: 16.0631
average: 3.21262
max: 5.4321
min: 1
ratio: 5.43
done ====================
代码如下:
"use strict";
function myMain() {
var num = Number(prompt("give a number of times to repeat, must be 0 or greater"));
var count = num;
for (var a=0; a<=num; a++) {count++;}
alert("Stats Solver execution " + num + " ===================");
if (num===0){alert("done ==================="); return;}
wall()
alert("done ===================");
}
function wall(){
var num1 = Number(prompt("provide a number"));
var num2 = Number(prompt("provide a second number"));
var num3 = Number(prompt("provide a third number"));
var num4 = Number(prompt("provide a fourth number"));
var num5 = Number(prompt("provide a fifth number"));
var sum = (num1+num2+num3+num4+num5);
alert("sum: " + sum);
var avg = (sum/5);
alert("average: " + avg);
var max = (Math.max(num1,num2,num3,num4,num5));
alert("max: " + max);
var min = (Math.min(num1,num2,num3,num4,num5));
alert("min: " + min);
var ratio = (max/min);
alert("ratio: " + Math.floor(ratio*100)/100);
}
myMain();
发布于 2018-06-18 21:38:18
嗯,你真的一点也不离谱。实际上,您的解决方案包含了所需的所有代码,只是其中一些代码放错了位置。我会把这个作为评论发布,但由于这是一个新的工作账号,我实际上不能发表评论,所以这里有一个完整的解决方案和解释。
您的墙功能,虽然恼人的所有警报实际上是正确的,不需要任何调整。也就是说,您可能希望尝试使用parseInt和parseFloat来确保获得有效的数字,但我假设这超出了赋值的范围。
现在来看看你的main函数。
var num = Number(prompt("give a number of times to repeat, must be 0 or greater"));
这是好的,并将提示用户输入一个数字,您可能再次想要测试您使用上述链接获得了一个有效的数字。
var count = num;
for (var a=0; a<=num; a++) {count++;}
alert("Stats Solver execution " + num + " ===================");
if (num===0){alert("done ==================="); return;}
wall()
alert("done ===================");
这就是事情开始瓦解的地方,也是我认为你有问题的地方。因此,我将逐行分解这一行,并解释每一行都在做什么,您可以将其与您认为它所做的进行比较。
var count = num;
这里没什么特别的,您只是创建了另一个变量来保存num变量中的值。有点多余,但并不是什么大问题。
for (var a=0; a<=num; a++) {count++;}
这一行似乎给您带来了最大的困惑。这是实际的循环,但在循环体内部{ ....}除了将1添加到计数(count++)之外,没有执行任何操作。如果我对赋值的理解正确的话,在这个循环中,你需要在警告'Stats Solver execution .....‘之后调用你的wall函数。一些东西。你所需要做的就是把你的函数调用移到这个循环里面。
if (num===0){alert("done ==================="); return;}
wall()
alert("done ===================");
这部分显然让你有点迷茫,只是尝试让它正常工作,即使在12+多年的开发之后,我仍然会写这样的代码;)。您真的不需要这样做,因为对wall()的实际调用对您来说已经很好了。
我很无聊,正在等待工作,所以为了成为一个完整的答案,这里有一个例子,说明你的代码应该是什么样子的。请不要只是交出来,而是试着真正理解我写的和你做的之间的区别,因为这些是一些非常基本的概念,如果你掩盖了这些概念,你的生活会变得更加艰难。随时可以问,这就是人们学习的方式。
function myMain(){
// get the number of repetitions from the user and cast as a number
var num = Number(prompt('Please enter the number of times to repeat'));
// loop from 0 to the number the user provided - 1 hence the <
for (var i = 0; i < num; i++){
// alert the iteration of the loop, you will notice i add 1 to num when we alert it; this is because it starts at 0 so the first time it displays it would show 'Stats Solver execution 0 ======' instead of 'Stats Solver execution 1 ======'
alert('Stats Solver execution ' + (num + 1) + ' ===============');
// call your wall function
wall();
// go back to the top of the loop
}
// alert that we are done.
alert('done===============')
}
// your wall function goes here
// call your myMain function to kick everything off
myMain();
只是为了好玩,你可能想看一下console.log,而不是alert,这样就不会让弹出窗口变得如此烦人。
如果我漏掉了什么,或者你对任何事情感到困惑,请不要犹豫,我会尽我所能回答的。
https://stackoverflow.com/questions/50917282
复制