提示条件操作

Prompt for conditional action

本文关键字:操作 条件 提示      更新时间:2023-09-26

我正在制作一个故事,但我不能让这个做我需要的。我想让它根据用户在提示符下输入"运行"还是"停留"来执行不同的操作。

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");
 };

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

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;
}

您可能还需要考虑一个备用策略,以防用户不输入这两个单词中的任何一个。

您需要将prompt的返回值赋给一个变量,并检查该变量的值是否与"Run"或"Stay"或其他值匹配

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
}