gpt4 book ai didi

javascript - 使用增量变量 c++ 与使用 count++。为什么一个计算失败,另一个执行?

转载 作者:行者123 更新时间:2023-11-30 20:52:57 25 4
gpt4 key购买 nike

大家好,我写这段代码主要是为了好玩/练习。它的基本目的是获取两个给定值之间的所有数字,并将它们与一个每次循环递增 1 的计数器进行比较,以确定这对数字是奇数、偶数还是混合数。除了校验函数中的一个变量名外,下面给出的两个代码完全相同。在第一个代码中,我使用变量 c 作为计数器,在第二个代码中使用了一个名为 count 的变量。出于某种原因,第一个代码拒绝计算,但第二个代码运行。我想知道这是为什么?

这是检查函数中带有变量 c 的第一段代码。这是一个故障:

<!DOCTYPE HTML>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>A Little Bit Of Everything</title>

<script type="text/javascript">

function tr() {

var oddn = parseFloat(document.getElementById("oddnum").value);
var odde = parseFloat(document.getElementById("oddend").value);

var compare = check(oddn, odde);

document.getElementById("output").innerHTML = compare;

document.getElementById("output1").value = compare;
}

function check(N, E) {


var c = 1;

var statement = " ";

for (var i = N; N <= E; N = N + i) {



var outcome = ifcheck(N, count);

statement = statement + N + " and " + count + " are a " + outcome;

c++;

}

return statement;

}

function ifcheck(count, Ncount) {

var message = " ";

if (count % 2 == 0 && Ncount % 2 == 0) {

message = "even pair. ";

}

else if (count % 2 != 0 && Ncount % 2 != 0) {

message = "odd pair. ";

}

else if ((count % 2 == 0 && Ncount % 2 != 0) || (count % 2 != 0 && Ncount % 2 == 0)) {

message = "mixed pair. ";

}

else {message = "incomplete."}

return message;

}

</script>
</head>

<body>

This code is meant to check for odd, even, and odd/even pairs between a counter that starts at 1 <br>
and an number odd number that adds its own value to itself until it reachs the maxium counter entered <br>
by the user. <br><br>

Please enter in an odd number. <input type="text" id="oddnum"><br>
Please enter in a maxium number for the loop to reach. <input type="text" id="oddend"><br><br>

<button type="button" onclick="tr()">Calculate Pairs</button>

<div id="output"></div>

<input type="text" id="output1" size="400">

</body>
</html>

这是第二个代码。除了在检查函数中将变量 c 重命名为 count 之外,它是相同的。这个有效:

<!DOCTYPE HTML>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>A Little Bit Of Everything</title>

<script type="text/javascript">

function tr() {

var oddn = parseFloat(document.getElementById("oddnum").value);
var odde = parseFloat(document.getElementById("oddend").value);

var compare = check(oddn, odde);

document.getElementById("output").innerHTML = compare;

document.getElementById("output1").value = compare;
}

function check(N, E) {


var count = 1;

var statement = " ";

for (var i = N; N <= E; N = N + i) {



var outcome = ifcheck(N, count);

statement = statement + N + " and " + count + " are a " + outcome;

count++;

}

return statement;

}

function ifcheck(count, Ncount) {

var message = " ";

if (count % 2 == 0 && Ncount % 2 == 0) {

message = "even pair. ";

}

else if (count % 2 != 0 && Ncount % 2 != 0) {

message = "odd pair. ";

}

else if ((count % 2 == 0 && Ncount % 2 != 0) || (count % 2 != 0 && Ncount % 2 == 0)) {

message = "mixed pair. ";

}

else {message = "incomplete."}

return message;

}

</script>
</head>

<body>

This code is meant to check for odd, even, and odd/even pairs between a counter that starts at 1 <br>
and an number odd number that adds its own value to itself until it reachs the maxium counter entered <br>
by the user. <br><br>

Please enter in an odd number. <input type="text" id="oddnum"><br>
Please enter in a maxium number for the loop to reach. <input type="text" id="oddend"><br><br>

<button type="button" onclick="tr()">Calculate Pairs</button>

<div id="output"></div>

<input type="text" id="output1" size="400">

</body>
</html>

c 是保留字还是有其他原因导致一个代码有效而另一个无效?

最佳答案

在您的第一个代码中,您已将变量声明为 c,但之后的每个地方都使用了变量 count。您可以通过将所有 count 替换为 c 来使第一个代码正常工作。

NOc 不是保留关键字。

关于javascript - 使用增量变量 c++ 与使用 count++。为什么一个计算失败,另一个执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47973731/

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