這個問題在這里已經有了答案: Javascript ( ) 符號連接而不是給出變數的總和 14 個答案 2天前關閉。
parseInt(sum3) parseInt(sum5)不是添加兩個變數。它只是將它們連接起來。我怎樣才能添加這兩個?
我知道我可以手動創建另一個變數并將 sum3 的輸出放在那里并將 sum5 的輸出放在另一個變數中并像這樣添加它們,但我認為這是作弊。那么如何添加 sum3 和 sum5 呢?
// sum of 3
let max3 = parseInt(1000);
let sum3 = 0;
for ( let three=3; three<=max3; three ){
sum3 =three
}
// sum of 5
let max5 = parseInt(1000);
let sum5 = 0
for (let five=5; five<=max5; five ){
sum5 =five
}
// sum of 3 and 5
document.write("Sum of 3 = " sum3 "<br/>"
"Sum of 5 = " sum5 "<br/>"
"Sum of 3 and 5 = " parseInt(sum3) parseInt(sum5));
uj5u.com熱心網友回復:
您可以只parseInt(sum3) parseInt(sum5)用 () 括號括起來,它應該可以完成這項作業。
// sum of 3
let max3 = parseInt(1000);
let sum3 = 0;
for (let three = 3; three <= max3; three ) {
sum3 = three
}
// sum of 5
let max5 = parseInt(1000);
let sum5 = 0
for (let five = 5; five <= max5; five ) {
sum5 = five
}
// sum of 3 and 5
document.write("Sum of 3 = " sum3 "<br/>"
"Sum of 5 = " sum5 "<br/>"
"Sum of 3 and 5 = " (parseInt(sum3) parseInt(sum5)));
uj5u.com熱心網友回復:
這只是因為您在 字串連接的背景關系中使用。
應該:
document.write("Sum of 3 = " sum3 "<br/>"
"Sum of 5 = " sum5 "<br/>"
"Sum of 3 and 5 = " (sum3 sum5));
或者你真的應該使用:
document.write(`Sum of 3 = ${sum3} <br/>
Sum of 5 = ${sum5} <br/>
Sum of 3 and 5 = ${sum3 sum5}`);
uj5u.com熱心網友回復:
您可以使用“ ”連接兩個字串值。
不要將其與字串結合使用,因此 ' ' 仍將按預期作業。
// sum of 3
let max3 = 1000;
let sum3 = 0;
for (let three=3; three<=max3; three ){
sum3 =three
}
// sum of 5
let max5 = 1000;
let sum5 = 0
for (let five=5; five<=max5; five ){
sum5 =five
}
// sum of 3 and 5
let totalSum = sum3 sum5
document.write("Sum of 3 = " sum3 "<br/>"
"Sum of 5 = " sum5 "<br/>"
"Sum of 3 and 5 = " totalSum);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/449392.html
標籤:javascript 循环 for循环
