当前位置: 代码迷 >> JavaScript >> 有挑战性的,if语句判断太繁琐,想改成switch,哪位能帮帮忙,该怎么解决
  详细解决方案

有挑战性的,if语句判断太繁琐,想改成switch,哪位能帮帮忙,该怎么解决

热度:522   发布时间:2012-02-03 22:02:47.0
有挑战性的,if语句判断太繁琐,想改成switch,哪位能帮帮忙
请问高手们,现在有三套电脑配置,有两种配置的价格一样,我这个只能判断两种,我想用switch来判断三种(或是多种),最后的价格,应该怎么做,请指教
有三种配置,应该会出现三种不同的价格,


<html>
<head>
<script   language= "javascript ">
function   get_price()
{
var   the_price=1000;
if(this.speed== "500MHZ ")
{
the_price+=200;
}
else
{
the_price+=100;
}

if(this.hdspace== "15GB ")
{
the_price+=200;
}
else
{
the_price+=100;
}
if(this.ram== "128MB ")
{
the_price+=150;
}
else
{
the_price+=75;
}

return   the_price;
}

function   computer(speed,hdspace,ram)
{
this.speed=speed;
this.hdspace=hdspace;
this.ram=ram;
this.price=get_price;
}
var   work_computer   =   new   computer( "500MHZ ", "15GB ", "128MB ");
var   home_computer   =   new   computer( "450MHZ ", "10GB ", "64MB ");
var   laptop_computer   =   new   computer( "350MHZ ", "7GB ", "32MB ");
var   work_computer_price   =   work_computer.price();
var   home_computer_price   =   home_computer.price();
var   laptop_computer_price   =   laptop_computer.price();
</script>
</head>
<body>
<script   language= "javascript ">
document.write( "这里是第一种配置work_computer <br> ");
document.write(work_computer.speed+ ", "+work_computer.hdspace+ ", "+work_computer.ram+ " <br> ");
document.write( "共$   "+work_computer_price+ " <br> ");
document.write( "这里是第二种配置home_computer <br> ");
document.write(home_computer.speed+ ", "+home_computer.hdspace+ ", "+home_computer.ram+ " <br> ");
document.write( "共$   "+home_computer_price+ " <br> ");
document.write( "这里是第三种配置laptop_computer <br> ");
document.write(laptop_computer.speed+ ", "+laptop_computer.hdspace+ ", "+laptop_computer.ram+ " <br> ");
document.write( "共$   "+laptop_computer_price   );
</script>

</script>
</body>
</html>

------解决方案--------------------
eval( "switch (this.speed){case \ "500MHZ\ ":the_price+=200;break;case \ "45MHZ\ ":the_price = ;break;case \ "350MHZ\ ":the_price = ;break;default:break;} ");
  相关解决方案