Javascript结合AND&&或|| switch语句中的条件

时间:2015-06-01 11:22:09

标签: javascript switch-statement

我正在用javascript创建一个switch语句。我想使用&&(AND)||(OR)条件。我希望var user = prompt接受输入141314km13km14 km13 km(与来自用户的空间)。我想对所有其他案件做同样的事情。

我该怎么做?

var user = prompt("How fast can you run per hour in kilometers (km)?").toUpperCase();

switch(user) {

case '14km':
    if ( 14 || 13 == true) {
    console.log("Woah!, your almost as fast as Usain Bolt!");
                           }
    else {
        console.log("RUN FASTER");
         }
    break;
case '12km':
    if ( 12 || 11 == true) {
    console.log("Your quick, but not as quick as me!");
                           }
    else {
        console.log("TOO SLOW");
         }
    break;
case '10km':
    if ( 10 && 9 )    {
    console.log("Average!");
             }
    break;
case '8km':
    console.log("OK tortoise!");
    break;
case '6km':
    console.log("I think it's better you get some rest");
    break;
default:
    console.log("I didn't understand you? Can you repeat that please");
}

4 个答案:

答案 0 :(得分:1)

您无法将case&&语句切换为||语句,但您可以为相同的代码指定多个case语句,如下所示:< / p>

switch(user) {
    case '14km':
    case '13km':
    case '14':
    case '13':
        console.log("Woah!, your almost as fast as Usain Bolt!");
        break;
    case '12km':
    case '11km':
    case '12':
    case '11':
        console.log("Your quick, but not as quick as me!");
        break;
    case '10km':
    case '9km':
    case '10':
    case '9':
        console.log("Average!");
        break;
    case '8km':
    case '8':
        console.log("OK tortoise!");
        break;
    case '6km':
    case '6':
        console.log("I think it's better you get some rest");
        break;
    default:
        console.log("I didn't understand you? Can you repeat that please");
}

答案 1 :(得分:0)

您可以使用switch语句代码流尝试一些技巧。

请参阅此小提琴,了解您的问题的示例 https://jsfiddle.net/p51h5y4f/1/

请参阅:

case 14:
case 13:
    console.log("Woah!, your almost as fast as Usain Bolt!");
    break;
case 12:
case 11:
    console.log("Your quick, but not as quick as me!");
    break;

在那里,用户输入一个int到框提示符,如果它是14或13,它会进入第一个响应,等等。

答案 2 :(得分:0)

您不能将操作符与switch一起使用。但是,您可以在单个块中评估多个案例。

//Using regular expression extract on number
//So that you don't need to evaluate (14 km/14 KM/14km/14KM/14)
user = user.replace(/[^\d.]/g, '');

//Convert to number
user = parseInt(user , 10);

switch(user) {
    case 14:
    case 13:
        console.log("Woah!, your almost as fast as Usain Bolt!");
        break;
    case 12:
    case 11:
        console.log("Your quick, but not as quick as me!");
        break;
    case 10:
    case 9:
        console.log("Average!");
        break;
    case 8:
        console.log("OK tortoise!");
        break;
    case 6:
        console.log("I think it's better you get some rest");
        break;
    default:
        console.log("I didn't understand you? Can you repeat that please");
}

答案 3 :(得分:-2)

  

希望var user = prompt接受输入14或13,或14km或13km,   或来自用户的14公里或13公里(带空格)

用户必须在html表单中输入值。然后你得到值

document.getElementById('KM').value