作者:罂粟花wd2010 | 来源:互联网 | 2023-09-23 19:17
对于此代码,我应该提示用户输入需要检查的值,并将其分为偶数,奇数和浮点数。用户将键入-1以退出循环。我应该使用一个while循环,它会循环成真。
最后,应该显示出奇/偶/浮点数的总和和平均值,以及浮点数本身的总和和平均值。
这应该是这样的:
- 4是一个偶数
- 5是一个奇数
- 0是偶数
- 12是一个偶数
- 10.29是浮点数
- 1.23是浮点数
- 23是一个奇数
总和为55.52,平均为7.93。浮点数的总和是11.52,平均值是5.76。
这是我到目前为止所拥有的:
function myValues() {
var num = 0;
var total = 0;
var average = 0;
var flnum = 0;
var fltotal = 0;
var flavg = 0;
var count = 0;
var sequence = document.getElementById("output");
var string = " ";
flnum= num;
while (true) {
num = parseFloat(prompt("Enter a value or enter -1 to quit: "));
count++
if (num != -1 && num%2 == 0) {
string += "" + num + " is an even number." + "";
}
if (num != -1 && num%2 != 0) {
string += "" + num + " is an odd number." + "";
}
if (num != -1 && num%1 != 0) {
flnum = num;
string += "" flnum.toFixed(2) + " is a floating-point number." + " ";
}
if (num == -1) {
break;
}
total = total + num;
fltotal = fltotal + flnum;
}
average = total/(count-1);
flavg = fltotal/(count-1);
string += "";
sequence.innerHTML = string;
document.getElementById("result").innerHTML = ("The sum is " + total + " and the average is " + average + ". The sum the floating point number is " + fltotal.toFixed(2) + " with an average of " + flavg.toFixed(2) + ".");
}