第1个回答 2023-03-20
<template>
<div>
<input type="number" v-model="num1" placeholder="请输入第一个数">
<input type="number" v-model="num2" placeholder="请输入第二个数">
<button @click="sumEven">计算偶数和</button>
<p>偶数和为: {{ evenSum }}</p>
</div>
</template>
<script>
export default {
data() {
return {
num1: '',
num2: '',
evenSum: 0,
}
},
methods: {
sumEven() {
const n1 = parseInt(this.num1);
const n2 = parseInt(this.num2);
let sum = 0;
for (let i = Math.min(n1, n2); i <= Math.max(n1, n2); i++) {
if (i % 2 === 0) {
sum += i;
}
}
this.evenSum = sum;
},
},
}
</script>