Javascript嵌套切换语句

时间:2017-12-31 03:49:41

标签: javascript

我是Javascript的新手,我正在学习switch语句。我的代码试图运行两层switch语句和验证数据。我想要的输出是让用户输入YES或NO,如果选择YES,那么它将通过switch语句,但是如果选择YES以外的任何东西,那么它将输出“Too Bad!”。到控制台。我有编译器错误,我不知道如何解决这个问题因为我没有完全理解javascript语法。

var user = prompt("Welcome to learning about exceptions with me, A-rod. 
In this tutorial we will be learning about exceptions and what not 
to do with them. Let's get started, shall we? ").toUpperCase(); 

switch(user){
    case 'YES'
     var user_1 = prompt("What's your name?"); 
     switch(user_1) {
        case 'Buster':
            console.log("Hey, brother!");
            break;
        case 'Alex':
            console.log("I've made a huge mistake.");
            break;
        case 'Steve':
            console.log("Steve Holt!");
            break;
     default:
         console.log("I don't know you!");}
break; 
default:
    console.log("too bad!"); 
 }

错误是

  

“期待':'代替'var'”

  

“期待'(结束)'而不是'默认'”

2 个答案:

答案 0 :(得分:0)

var user = prompt("Welcome to learning about exceptions with me, A-rod." + 
"In this tutorial we will be learning about exceptions and what not" +  
"to do with them. Let's get started, shall we? ").toUpperCase(); 

switch(user){
    case 'YES':
     var user_1 = prompt("What's your name?"); 
     switch(user_1) {
        case 'Buster':
            console.log("Hey, brother!");
            break;
        case 'Alex':
            console.log("I've made a huge mistake.");
            break;
        case 'Steve':
            console.log("Steve Holt!");
            break;
     default:
         console.log("I don't know you!");
         break;
     }
break;
default:
    console.log("too bad!"); 
    break;
 }

答案 1 :(得分:0)

您的第一个switch语句缺少分号:

var user = prompt("Welcome to learning about exceptions with me, A-rod. In this tutorial we will be learning about exceptions and what not to do with them. Let's get started, shall we? ").toUpperCase(); 

switch(user){
    case 'YES':
     var user_1 = prompt("What's your name?"); 
     switch(user_1) {
        case 'Buster':
            console.log("Hey, brother!");
            break;
        case 'Alex':
            console.log("I've made a huge mistake.");
            break;
        case 'Steve':
            console.log("Steve Holt!");
            break;
     default:
         console.log("I don't know you!");}
break; 
default:
    console.log("too bad!"); 
 }