gpt4 book ai didi

JavaScript 没有正确计算重复项

转载 作者:行者123 更新时间:2023-12-03 11:44:03 24 4
gpt4 key购买 nike

我编写了一个 JavaScript 来要求用户输入 5 个数字并检查最大和最小的数字。我有时可以让它显示最大和最小的数字。主要问题是,当用户多次输入最小或最大数字时,我需要显示错误消息。当我在前两个框中输入两个大数字时,我会收到一条错误消息,但如果我输入两个小数字,我不会收到错误消息。这是我的代码,我怀疑这是我的 else if,但我不完全确定。

编辑:抱歉,我忘了提及我只能使用 if-else 语句。没有循环,没有数组。都不是。我知道,我知道,这绝对是可怕的。

function testProgram() {

var largestNum, smallestNum;
var num1, num2, num3, num4, num5;
var smallDupe, largeDupe;

var num1 = document.getElementById("Num1").value;
var num2 = document.getElementById("Num2").value;
var num3 = document.getElementById("Num3").value;
var num4 = document.getElementById("Num4").value;
var num5 = document.getElementById("Num5").value;

num1 = parseFloat(num1);
num2 = parseFloat(num2);
num3 = parseFloat(num3);
num4 = parseFloat(num4);
num5 = parseFloat(num5);

if ((!isNaN(num1)) && (!isNaN(num2)) && (!isNaN(num3)) && (!isNaN(num4)) && (!isNaN(num5))) {

largestNum = num1;
smallestNum = num1;
smallDupe = 0;
largeDupe = 0;

if(num2 >= largestNum) {
if(num2 == largestNum) {
largeDupe++;
}
else if (num2 <= smallestNum) {
if(num2 == smallestNum) {
smallDupe++;
}
smallestNum = num2;
smallDupe = 0;
} else {
largestNum = num2;
largeDupe = 0;
}
}
if(num3 >= largestNum) {
if(num3 == largestNum) {
largeDupe++;
}
else if (num3 <= smallestNum) {
if(num3 == smallestNum) {
smallDupe++;
}
smallestNum = num3;
smallDupe = 0;
} else {
largestNum = num3;
largeDupe = 0;
}
}
if(num4 >= largestNum) {
if(num4 == largestNum) {
largeDupe++;
}
else if (num4 <= smallestNum) {
if(num4 == smallestNum) {
smallDupe++;
}
smallestNum = num4;
smallDupe = 0;
} else {
largestNum = num4;
largeDupe = 0;
}
}
if(num5 >= largestNum) {
if(num5 == largestNum) {
largeDupe++;
}
else if (num5 <= smallestNum) {
if(num5 == smallestNum) {
smallDupe++;
}
smallestNum = num5;
smallDupe = 0;
} else {
largestNum = num5;
largeDupe = 0;
}
}

if (smallDupe > 0 || largeDupe > 0) {

// Display an error to the user stating that there are duplicates
window.alert("The smallest number and/or largest number contains is duplicated.");
console.log("Error notice");
}

// Reference out1 and out2 to the HTML document
var out1 = document.getElementById("Out1");
var out2 = document.getElementById("Out2");
out1.disabled = false;
out1.value = largestNum;
out2.disabled = false;
out2.value = smallestNum;

} else {

// Tells the user that their input is invalid and to input five numbers again
window.alert("Please input five numbers");
console.log("Error notice");

}
}

最佳答案

增加 smallDupe 后,您将其设置回 0。您需要分别处理当前数字小于最小数字和等于数字的情况。另外,如果您在第一个框中输入相同的数字,则它将是最大和最小的重复,您需要允许这样做;最简单的方法是使用两组独立的 if/else if,而不是将它们全部组合成一个大语句。

if(num2 > largestNum) {
largestNum = num2;
largeDupe = 0;
} else if (num2 == largestNum) {
largeDupe++;
}

if (num2 < smallestNum) {
smallestNum = num2;
smallDupe = 0;
} else if (num2 == smallestNum) {
smallDupe++;
}

关于JavaScript 没有正确计算重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26135173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com