Javascript - 无法让我的程序运行 - 循环永不结束

时间:2015-12-07 16:23:54

标签: javascript function switch-statement parameter-passing case

我的程序运行不正常。它应该提示您输入您的名字并存储它 - 然后要求您选择选项1,2或3(退出并返回选择) - do-while循环。那时我的功能没有被调用,例如如果用户键入1然后调用Opt1,则调用用户类型2 Opt2。如果任何人都可以提供帮助,那就完全失败了。

<html>
<head>
    <script type='text/javascript'>

        var me;
        function Menu () {
            var choice;
            me = prompt("Who are you?"," ") ;

            do {
                prompt("Choose option 1, 2 or 3" + choice);
                choice = parseInt(choice);

                switch (choice) {
                    case 1: {
                        Opt1();
                        break;
                    }
                    case 2: {
                        Opt2();
                        break;
                    }
                    case 3: {
                        alert("All done");
                        break;
                    }
                } //end of switch
            } //end of do-while

            while (choice != 3); {
                break;
            }

        } //end Menu

        function Opt1()
        {
            var user;
            alert(me + "This is Option 1") ;
            user = prompt("Enter first and last name");
            alert('Welcome ' + create_name(user));
            create_name(user);
        }

        function Opt2()
        {
            alert(me + 'This is Option 2' + me);
        }

        function create_name(username) { //string manipulation
            var string1 = username.substr(0,1);
            alert("Hello" + string1);
            var len = username.length;
            alert("the number of letters in your name is " + len);
            var pos = username.indexOf(" ");

            alert("the space is located at position " + pos);
            var string2 = username.substr(pos+1);
            username = string1+string2;

            alert ('Welcome ' + username);
        }

    </script>
</head>
<body>
    <h1> Software Program</h1>
    <input type="button" value="Menu" onclick="Menu();" />
</body>
</html>

1 个答案:

答案 0 :(得分:0)

@CodeiSir是对的,但真正的问题是你没有收集用户输入的值。

更改此

                prompt("Choose option 1, 2 or 3" + choice);

用这个:

                choice = prompt("Choose option 1, 2 or 3");

此致