gpt4 book ai didi

javascript - 在提示中输入 3 个数字并在 JavaScript 中获取最大值

转载 作者:行者123 更新时间:2023-12-02 17:17:11 26 4
gpt4 key购买 nike

我对此很陌生。我正在为一个类做练习,正如主题所述,我试图让用户在出现的提示窗口中输入 3 个数字。这工作正常,但我试图让输入的最高数字在提示结束后出现。正如我在代码中所指出的,可以在 Math.max 中添加任何内容来使其工作吗?它非常接近工作,但还没有完全实现。我感谢任何帮助。

<!DOCTYPE html>
<html lang="en">
<head>
<title>
Number Input with Prompt
</title>
<script type = "text/javascript">
<!--
var total;
var inputNumber;
var value;
var highestNumber;
var finalMax = Math.max. //<-----Can anything be added here to get the maximum number entered in the prompt window that appears?
total = 0;
inputNumber = 1;

while ( inputNumber <= 3 ){
highestNumber = window.prompt( "Enter number " + inputNumber + " of 3:" , "" );
while(isNaN(value = parseInt( highestNumber ))){
highestNumber = window.prompt( "Enter number " + inputNumber + " of 3:" , "" );
}
total = total + value;
inputNumber = inputNumber + 1;
}
document.writeln(
"<h1>The highest number is " + finalMax + "</h1>" );
// -->
</script>
</head>
<body>
</body>
</html>

最佳答案

此方法将响应与最新的最高响应进行比较。

逻辑=“如果值>最高编号,最高编号=值”

<script type = "text/javascript">

<!--

var total;
var inputNumber;
var value;
var highestNumber;
//var finalMax = Math.max. //<-----Can anything be added here to get the maximum number entered in the prompt window that appears?

total = 0;
inputNumber = 1;

while ( inputNumber <= 3 ){

value = window.prompt( "Enter number " + inputNumber + " of 3:" , "" );

while(isNaN(parseInt( value ))){
value = window.prompt( "Enter number " + inputNumber + " of 3:" , "" );
}

if (highestNumber == undefined || highestNumber < value) {
highestNumber = value;
}

total = total + value;

inputNumber = inputNumber + 1;

}



document.writeln(
"<h1>The highest number is " + highestNumber + "</h1>" );

// -->

</script>

关于javascript - 在提示中输入 3 个数字并在 JavaScript 中获取最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24332031/

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