(Javascript)从用户获取三个整数以显示总和,平均值,产品,最小和最大数量

时间:2015-12-19 02:45:12

标签: javascript algorithm math dialog average

我遇到的第一个问题是显示三个数字中最小和最大的一个。出现两个单独的警报 - 第一个警报表示第二大警报是最大的(因为它尚未考虑第三个数字),第二个警报正确地指出三个中的最大数量是最大的。不知道为什么会这样 - 我只需要一个警报。

我遇到的第二个问题是显示总和,平均值和产品。当我尝试运行此代码时,页面就会变成空白 - 它根本不起作用。现在这部分代码被注释掉了。如果你知道问题是什么,请帮忙。我将需要所有这些信息显示在同一个警报中。谢谢。 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 个答案:

答案 0 :(得分:3)

请在整个答案中。我将逻辑嵌入到函数中,并在找到所有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>

&#13;
&#13;
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);
}
&#13;
<h1 style = "text-align:center">Compare the Integers!</h1>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

//  Initialising variables
var number1, number2, number3, sum, average, product, largest, smallest;

//Inpuing the Numbers 
// parseInt is used to covert the var to integer value or else the result will be concatenation of all the values
number1 = parseInt(window.prompt('Enter Number 1 :'));
number2 = parseInt(window.prompt('Enter Number 2 :'));
number3 = parseInt(window.prompt('Enter Number 3 :'));

// Adding the Numbers
sum = number1 + number2 + number3;

// Finding the average
average = parseInt(sum) / 3;

// Finding Product 
product = number1 * number2 * number3;

// Checking the Largest value
if (number1 > number2 && number1 > number3) {
    largest = number1;
} else if (number2 > number1 && number2 > number3) {
    largest = number2;
} else {
    largest = number3;
}

//Checking the smallest value

if (number1 < number2 && number1 < number3) {
    smallest = number1;
} else if (number2 < number1 && number2 < number3) {
    smallest = number2;
} else {
    smallest = number3;
}

//Returning or Printing Result in Alert box
window.alert('Sum : ' + sum + '\nAverage : ' + average.toFixed(2) + '\nProduct : ' + product + '\nLargest : ' + largest + '\nSmallest : ' + smallest);

答案 2 :(得分:0)

<html>
<script>

     var firstNumber; 
     var secondNumber;
     var thirdNumber;

     firstNumber = window.prompt("Enter number");

     secondNumber = window.prompt("Enter another number");

     thirdNumber = window.prompt("Enter third number");



     if (firstNumber > secondNumber && firstNumber > thirdNumber)
        window.alert(firstNumber + " is largest");

     else if (secondNumber > firstNumber && secondNumber > thirdNumber)
        window.alert(secondNumber + " is largest"); 

     else if (thirdNumber > firstNumber && thirdNumber > secondNumber)
        window.alert(thirdNumber + " is largest");

</script>
</html>