我正在尝试创建一个要求一定数量的欧元的提示,之后,根据输入它会计算出的数量,这是我试过但它总是显示默认值,怎么能我解决了吗?
var vraag = prompt("Omzet:");
var cijfer = parseInt(vraag);
switch(cijfer) {
case cijfer >= 100:
document.write(cijfer + "Kortingsbedrag = " + cijfer/100*10 + " euro" + "(Kortingspercentage is 10%)");
break;
case cijfer >= 500:
document.write(cijfer + "Kortingsbedrag = " + cijfer/100*15 + " euro" + "(Kortingspercentage is 15%)");
break;
case cijfer >= 1000:
document.write(cijfer + "Kortingsbedrag = " + cijfer/100*20 + " euro" + "(Kortingspercentage is 20%)");
break;
case cijfer >= 2500:
document.write(cijfer + "Kortingsbedrag = " + cijfer/100*25 + " euro" + "(Kortingspercentage is 25%)");
break;
case cijfer >= 10000:
document.write(cijfer + "Kortingsbedrag = " + cijfer/100*30 + " euro" + "(Kortingspercentage is 30%)");
break;
default:
document.write(cijfer + "Kortingsbedrag = " + cijfer/100*0 + " euro" + "(Kortingspercentage is 0%)");
}
答案 0 :(得分:1)
由于switch
和switch
值的严格比较,您的case
语句需要案例值。
您在案例部分中进行了比较,并且需要针对true
进行测试。
switch (true) {
答案 1 :(得分:0)
Nina Scholz的回答绝对正确。
然而,因为你似乎没有很多编程经验(如果我错了,请原谅我)。我想补充一些一般性的建议:
您的代码如下所示:
let vraag = prompt("Omzet:");
let cijfer = Number.parseInt(vraag);
let discount = 0;
switch (true) {
case cijfer >= 100:
discount = 0.1; //10%
break;
case cijfer >= 500:
discount = 0.15; //15%
break;
case cijfer >= 1000:
discount = 0.2; //20%
break;
case cijfer >= 2500:
discount = 0.25; //25%
break;
case cijfer >= 10000:
discount = 0.30; //30%
break;
}
document.write(`${cijfer} Kortingsbedrag = ${cijfer*discount} euro (Kortingspercentage is ${discount*100}%)`);
我希望它有所帮助! 祝你好运!