我不知道,否则/切换得很好,我不知道你是否可以将它们结合起来,我不确定是不是这样,或者我只是做错了,但是我花了好几个小时才找到问题而且我找不到它。以下是整个代码:
const Fanta = 250,Sprite = 250,Cola = 250,Dirol = 450,Snickers = 300,Lays = 800;
var a = prompt("1:Fanta = 250, 2:Sprite = 250, 3:Coca Cola = 250, 4:Dirol = 450, 5:Snickers = 300, 6:Lays = 800");
function math()
{
switch(a)
{
case 1:
alert("You chose Fanta");
var money = prompt("Put money in");
alert("You put in " + money)
if(money >= 250)
{
alert("You just purchased a Fanta! Take back " + (money - Fanta));
}
default:
{
alert("Error! You didn't put in enough money")
}
}
switch(a)
{
case 2:
alert("You chose Sprite");
var money = prompt("Put money in");
alert("You put in " + money)
if(money >= 250)
{
alert("You just purchased a Sprite! Take back " + (money - Sprite));
}
else
{
alert("Error! You didn't put in enough money")
}
}
switch(a)
{
case 3:
alert("You chose Cola");
var money = prompt("Put money in");
alert("You put in " + money)
if(money >= 250)
{
alert("You just purchased a Coca Cola! Take back " + (money - Cola));
}
else
{
alert("Error! You didn't put in enough money")
}
}
switch(a)
{
case 4:
alert("You chose Dirol");
var money = prompt("Put money in");
alert("You put in " + money)
if(money >= 450)
{
alert("You just purchased a Dirol! Take back " + (money - Dirol));
}
else
{
alert("Error! You didn't put in enough money")
}
}
switch(a)
{
case 5:
alert("You chose Snickers");
var money = prompt("Put money in");
alert("You put in " + money);
if(money >= 300)
{
alert("You just purchased a Snickers! Take back " + (money - Snickers));
}
else
{
alert("Error! You didn't put in enough money")
}
}
switch(a)
{
case 6:
alert("You chose Lays");
var money = prompt("Put money in");
alert("You put in " + money)
if(money >= 800)
{
alert("You just purchased a Sprite! Take back " + (money - Lays));
}
else
{
alert("Error! You didn't put in enough money")
}
}
}
math();
答案 0 :(得分:1)
你应该更好地优化你的代码...默认只能在交换机中使用,并且在你的代码中是没有饮料的选项。答案是你可以这样做: 被选中......
var enough_money=false; var diff=0; var drink="";
switch(a){
case(1):
alert(...);
if(...){
enough_money=true;
diff= money-cola;
drink= "Cola";
}
break;
case(2):
alert(...);
if(...){
enough_money=true;
diff= money-cola;
drink= "Cola";
}
break;
default:
alert("Please choose!");
}
if(enough_money){
alert(drink+ ' rest money'+diff);
}
答案 1 :(得分:0)
你写的东西像
switch(a) {
// switch block
}
switch(a) {
// another switch block
}
// and so on
我认为你需要将所有切换块合并为一个。并且不要忘记将break
放在case:
块中
所以你的代码看起来像这样:
switch(a) {
case x:
// case block 1
break;
case y:
// case block 2
break;
// other cases
default:
// if no one case is matched
}
答案 2 :(得分:0)
为了编写大量案例,您不需要多次编写开关(a)。您唯一需要做的就是在单个switch()中编写所有案例,并在该案例结尾添加 break; 。
编写代码的正确方法是:
const Fanta = 250,Sprite = 250,Cola = 250,Dirol = 450,Snickers = 300,Lays = 800;
var a = prompt("1:Fanta = 250, 2:Sprite = 250, 3:Coca Cola = 250, 4:Dirol = 450, 5:Snickers = 300, 6:Lays = 800");
function math()
{
switch(a)
{
case 1:
alert("You chose Fanta");
var money = prompt("Put money in");
alert("You put in " + money)
if(money >= 250)
{
alert("You just purchased a Fanta! Take back " + (money - Fanta));
}
break;
default:
{
alert("Error! You didn't put in enough money")
}
break;
case 2:
alert("You chose Sprite");
var money = prompt("Put money in");
alert("You put in " + money)
if(money >= 250)
{
alert("You just purchased a Sprite! Take back " + (money - Sprite));
}
else
{
alert("Error! You didn't put in enough money")
}
break;
case 3:
alert("You chose Cola");
var money = prompt("Put money in");
alert("You put in " + money)
if(money >= 250)
{
alert("You just purchased a Coca Cola! Take back " + (money - Cola));
}
else
{
alert("Error! You didn't put in enough money")
}
break;
case 4:
alert("You chose Dirol");
var money = prompt("Put money in");
alert("You put in " + money)
if(money >= 450)
{
alert("You just purchased a Dirol! Take back " + (money - Dirol));
}
else
{
alert("Error! You didn't put in enough money")
}
break;
case 5:
alert("You chose Snickers");
var money = prompt("Put money in");
alert("You put in " + money);
if(money >= 300)
{
alert("You just purchased a Snickers! Take back " + (money - Snickers));
}
else
{
alert("Error! You didn't put in enough money")
}
break;
case 6:
alert("You chose Lays");
var money = prompt("Put money in");
alert("You put in " + money)
if(money >= 800)
{
alert("You just purchased a Sprite! Take back " + (money - Lays));
}
else
{
alert("Error! You didn't put in enough money")
}
break;
}
}
math();
答案 3 :(得分:0)
switch(expression) {
case n:
code block
break;
case n:
code block
break;
default:
default code block
}