提示有条件的行动

时间:2015-08-11 23:14:22

标签: javascript

我正在制作一个故事,但我不能让它做我需要的。我希望它根据用户是否在提示符下键入“运行”或“停留”来执行不同的操作。

confirm("Are You Ready To Play");
var age = prompt("How Old Are You");
if (age < 13);
{
 console.log("You Can Still Play But We Are No Responsible");
};
if (age > 13)
{ 
console.log("Great You Are At The Recommended Age");
};
console.log("You Are At A Famous Concert With Katy Perry And She starts   
singing Im Hot And Im Cold Im Yes And Im No Then She looks at You with a  
Weird Face And She like wait Stop The Music"); 
var Run = prompt("Do You Run Or Stay");    
if (Run)
{
console.log("You Run Then The Cops Are Called What Do You Do Now A Police    
Sais It's Time For Your Trial You Enter Court With a attorney But Your  
attorney failed you and you were declared GUILTY The End Try For Another  
Ending");
}
else if (Stay)
{
 console.log("We'll You Decided To Stay, well Katy perry looks At You And  
 Sais with a :) get up here and let's sing you are like WTF is happening   
 plus you get a backstage pass");
 };

2 个答案:

答案 0 :(得分:0)

您必须比较用户输入并执行相应的操作。例如。使用switch statement

var action = prompt('Do You Run Or Stay?');
switch (action.toLowerCase()) {
  case 'run': 
    // do stuff on run
    break;
  case 'stay':
    // do stuff on stay
    break;
}

如果用户没有输入任何一个单词,您可能还想考虑一个后备策略。

答案 1 :(得分:0)

您需要将prompt的返回值赋给变量,并检查该变量的值是否与&#34; Run&#34;或者&#34;留下&#34;或其他什么

var answer = prompt("Do You Run Or Stay");    
if (answer == "Run") {
    // Run code here
}
else if (answer == "Stay") {
   // Stay code here
}
else {
   // User wrote something else
}