-5

我有一个无法工作的简单 js

我需要为每个变量显示相应的值 ie 玩具总动员 3 显示喜剧精彩选择等等

问题似乎出在 if else 语法中

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>help</title>
</head>

<body>
<script type="text/javascript">

var movie = prompt("Select your favorite movie").toLowerCase();

if(movie =="toy story 3","kung fu Panda","RIO");
{
document.write("<p>Comedy splendid choice</p>")
}
else if(movie ="sex in the city","the backup plan","twilight");
{
document.write("<p>Chick flicks are always fun</p>")
}
else if(movie ="fast 5" || movie=="the karate kid");
{
document.write("<p>Action is satisfaction/p>")
}
else
{
document.write("<p>I’m sure it’s a good movie I just don’t know about it/p>")
}

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

4 回答 4

2

首先,;在 endif else语句中删除。

二、使用||创造或逻辑表达。

第三,您的输入是小写的,因此也将您的输入movie与小写文字进行比较。

第四,==在比较相等时使用。=用于变量赋值

尝试这个:

var movie = prompt("Select your favorite movie").toLowerCase();

if(movie =="toy story 3" || movie == "kung fu panda" || movie=="rio")
{
   document.write("<p>Comedy splendid choice</p>");
}
else if(movie =="sex in the city" || movie == "the backup plan" || movie == "twilight")
{
   document.write("<p>Chick flicks are always fun</p>");
}
else if(movie =="fast 5" || movie=="the karate kid")
{
   document.write("<p>Action is satisfaction/p>");
}
else
{
   document.write("<p>I’m sure it’s a good movie I just don’t know about it/p>");
}
于 2013-04-15T01:02:00.347 回答
0

您需要创建一个或:

if(movie =="toy story 3" || movie == "kung fu Panda" || movie == "RIO")
{
document.write("<p>Comedy splendid choice</p>")
}
.
.
.
于 2013-04-15T00:58:41.873 回答
0

“不工作”是什么意思?你能详细说明一下吗?

如果您的意思没有显示,请检查您的代码:

<script type="text/javascript">
    var movie = prompt("Select your favorite movie").toLowerCase();

    if(movie =="toy story 3","kung fu Panda","RIO")
    {
        document.write("<p>Comedy splendid choice</p>");
    }
    else if(movie ="sex in the city","the backup plan","twilight")
    {
        document.write("<p>Chick flicks are always fun</p>");
    }
    else if(movie ="fast 5" || movie=="the karate kid")
    {
        document.write("<p>Action is satisfaction/p>");
    }
    else
    {
        document.write("<p>I’m sure it’s a good movie I just don’t know about it/p>");
    }
</script>

您错过了分号 (;)。您应该在语句的末尾加上分号,而不是在 if case 之后。

应该:

if ( condition == true) 
{
    document.write("Hey, it's true!");
}

不是:

if ( condition == true); // Semi-colon here means, it's the end of the statement
                         // the code after won't be executed
{
    document.write("Hey, it's true!"); // Semi-colon should be here
}
于 2013-04-15T01:03:35.393 回答
-1

使用 switch 语句而不是多个 if then else...

switch(movie)
{
    case "Toy story":
        document.write("<p>Comedy splendid choice</p>");
        break;

    case "fast 5":
        document.write("<p>Action is satisfaction/p>");
        break;
}
于 2013-04-15T01:05:18.240 回答