我遇到的第一个问题是显示三个数字中的最小和最大.出现两个单独的警报-第一个警报说第二个最大的数字是最大的(因为尚未考虑第三个数字),第二个警报正确地指出三个中最大的数字是最大的.不知道为什么会这样-我只需要一个警报.
我遇到的第二个问题是显示总和,平均值和乘积.当我尝试运行此代码时,页面只是空白-根本不起作用.现在,这部分代码已被注释掉.如果您知道问题所在,请提供帮助.我将需要所有这些信息来显示在同一警报中.谢谢. JSFiddle:https://jsfiddle.net/HappyHands31/xzsf8ca4/8/
<!DOCTYPE html>
<!-- Fig. 6.14: welcome5.html -->
<!-- Using equality and relational operators. -->
<html>
<head>
<meta charset = "utf-8">
<title>Compare Numbers</title>
<h1 style = "text-align:center">Compare the Integers!</h1>
<script>
var firstNumber; // first string entered by the user
var secondNumber; // second string entered by the user
var thirdNumber; // third string entered by the user
var sum; // add the integers
var average; // add the integers, divide by 3
var product; // multiply the integers
// enter first number from user as a string
firstNumber = window.prompt("Enter first integer");
// enter second number from user as a string
secondNumber = window.prompt("Enter second integer");
// enter third number from user as a string
thirdNumber = window.prompt("Enter third integer");
// convert numbers from strings to integers
firstNumber = parseInt(firstNumber);
secondNumber = parseInt(secondNumber);
thirdNumber = parseInt(thirdNumber);
//determine result
if (firstNumber > secondNumber && thirdNumber)
window.alert(firstNumber + " is largest");
if (secondNumber > firstNumber && thirdNumber)
window.alert(secondNumber + " is largest");
if (thirdNumber > firstNumber && secondNumber)
window.alert(thirdNumber + " is largest");
//determine sum, average, and product
/*
sum (firstNumber + secondNumber + thirdNumber)
window.alert("the sum is ");
average ((firstNumber + secondNumber + thirdNumber) / 3))
window.alert("the average is ");
product (firstNumber * secondNumber * thirdNumber)
window.alert("the product is");
*/
// end if
</script>
</head>
<body>
</body>
</html>
解决方法:
请贯穿此答案.我将逻辑嵌入到函数中,并在找到所有3个数字后调用,并且逻辑也发生了变化
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- Fig. 6.14: welcome5.html -->
<!-- Using equality and relational operators. -->
<html>
<head>
<title>Compare Numbers</title>
<script type="text/javascript">
var firstNumber, // first string entered by the user
secondNumber, // second string entered by the user
thirdNumber, // third string entered by the user
sum, // add the integers
average, // add the integers, divide by 3
product; // multiply the integers
// enter first number from user as a string
firstNumber = window.prompt("Enter first integer");
// enter second number from user as a string
secondNumber = window.prompt("Enter second integer");
// enter third number from user as a string
thirdNumber = window.prompt("Enter third integer");
// convert numbers from strings to integers
firstNumber = parseInt(firstNumber);
secondNumber = parseInt(secondNumber);
thirdNumber = parseInt(thirdNumber);
findLargNum(firstNumber,secondNumber,thirdNumber);
function findLargNum(firstNumber,secondNumber,thirdNumber){
//determine result
if (firstNumber > secondNumber) {
if(firstNumber > thirdNumber) window.alert(firstNumber + " is largest");
}
else if(secondNumber > thirdNumber) window.alert(secondNumber + " is largest");
else window.alert(thirdNumber + " is largest");
var sum = firstNumber + secondNumber + thirdNumber;
window.alert("the sum is " + sum);
var average = (firstNumber + secondNumber + thirdNumber) / 3;
window.alert("the average is " + average);var product = firstNumber * secondNumber * thirdNumber;window.alert("the product is" + product);
}
</script>
</head>
<body>
<h1 style = "text-align:center">Compare the Integers!</h1>
</body>
</html>
var firstNumber, // first string entered by the user
secondNumber, // second string entered by the user
thirdNumber, // third string entered by the user
sum, // add the integers
average, // add the integers, divide by 3
product; // multiply the integers
// enter first number from user as a string
firstNumber = window.prompt("Enter first integer");
// enter second number from user as a string
secondNumber = window.prompt("Enter second integer");
// enter third number from user as a string
thirdNumber = window.prompt("Enter third integer");
// convert numbers from strings to integers
firstNumber = parseInt(firstNumber);
secondNumber = parseInt(secondNumber);
thirdNumber = parseInt(thirdNumber);
findLargNum(firstNumber,secondNumber,thirdNumber);
function findLargNum(firstNumber,secondNumber,thirdNumber){
//determine result
if (firstNumber > secondNumber) {
if(firstNumber > thirdNumber) window.alert(firstNumber + " is largest");
}
else if(secondNumber > thirdNumber) window.alert(secondNumber + " is largest");
else window.alert(thirdNumber + " is largest");
var sum = firstNumber + secondNumber + thirdNumber;
window.alert("the sum is " + sum);
var average = (firstNumber + secondNumber + thirdNumber) / 3;
window.alert("the average is " + average);var product = firstNumber * secondNumber * thirdNumber;window.alert("the product is" + product);
}
<h1 style = "text-align:center">Compare the Integers!</h1>