如何在javascript中将case语句放入循环中
您好,我正在寻找一些关于如何将这个switch语句转换成do循环的建议。string函数和number函数都执行其他程序,并且必须执行到按下3为止。任何帮助都将不胜感激如何在javascript中将case语句放入循环中,javascript,loops,switch-statement,Javascript,Loops,Switch Statement,您好,我正在寻找一些关于如何将这个switch语句转换成do循环的建议。string函数和number函数都执行其他程序,并且必须执行到按下3为止。任何帮助都将不胜感激 function Menu() { var menu =0; document.write(" menu options " + name + "<br>"); document.write("option 1 stringFu
function Menu()
{
var menu =0;
document.write(" menu options " + name + "<br>");
document.write("option 1 stringFunction<br>");
document.write("option 2 numberFunction<br>");
document.write("option 3 goodbye<br>");
menu = prompt("please select a number between 1 and 3",0);
menu = parseInt(menu)
switch (menu)
{ // begin switch
case 1:
// begin case 1
document.write(name + " This is option 1<br> ") ;
stringFunction()
break ;
// end case 1
case 2:
// begin case 2
document.write(name + " This is option 2<br>") ;
numberFunction()
break ;
// end case 2
case 3:
// begin case 3
document.write('Goodbye ' + name) ;
break ;
// end case 3
default :
{// begin default
alert ("You must choose either 1,2,or 3");
}
} // end switch
} // end function
功能菜单()
{
var菜单=0;
文档。写入(“菜单选项”+名称+”
”;
编写(“选项1 stringFunction
”;
文件。写入(“选项2数字功能
”;
文件。写入(“选项3再见
”;
菜单=提示(“请选择一个介于1和3之间的数字”,0);
菜单=parseInt(菜单)
开关(菜单)
{//开始开关
案例1:
//开始案例1
文件。写入(名称+“这是选项1
”;
stringFunction()
打破
//结案1
案例2:
//开始案例2
文件。写入(名称+“这是选项2
”;
numberFunction()
打破
//结束情况2
案例3:
//开始案例3
文件。写(‘再见’+姓名);
打破
//结束情况3
违约:
{//开始默认设置
警报(“您必须选择1、2或3”);
}
}//结束开关
}//结束函数
我会使用while或do while循环。在您的条件下,检查输入的值是否不是3
//边走边挡
do{
menu = prompt("please select a number between 1 and 3",0);
menu = parseInt(menu)
//more code
}while(menu!=3);
//完整代码
function Menu()
{
var menu =0;
document.write(" menu options " + name + "<br>");
document.write("option 1 stringFunction<br>");
document.write("option 2 numberFunction<br>");
document.write("option 3 goodbye<br>");
do{
menu = prompt("please select a number between 1 and 3",0);
menu = parseInt(menu)
switch (menu)
{ // begin switch
case 1:
// begin case 1
document.write(name + " This is option 1<br> ") ;
stringFunction()
break ;
// end case 1
case 2:
// begin case 2
document.write(name + " This is option 2<br>") ;
numberFunction()
break ;
// end case 2
case 3:
// begin case 3
document.write('Goodbye ' + name) ;
break ;
// end case 3
default :
{// begin default
alert ("You must choose either 1,2,or 3");
}
}while(menu!=3);
} // end switch
} // end function
功能菜单()
{
var菜单=0;
文档。写入(“菜单选项”+名称+”
”;
编写(“选项1 stringFunction
”;
文件。写入(“选项2数字功能
”;
文件。写入(“选项3再见
”;
做{
菜单=提示(“请选择一个介于1和3之间的数字”,0);
菜单=parseInt(菜单)
开关(菜单)
{//开始开关
案例1:
//开始案例1
文件。写入(名称+“这是选项1
”;
stringFunction()
打破
//结案1
案例2:
//开始案例2
文件。写入(名称+“这是选项2
”;
numberFunction()
打破
//结束情况2
案例3:
//开始案例3
文件。写(‘再见’+姓名);
打破
//结束情况3
违约:
{//开始默认设置
警报(“您必须选择1、2或3”);
}
}while(菜单!=3);
}//结束开关
}//结束函数
我会使用while或do while循环。在您的条件下,检查输入的值是否不是3
//边走边挡
do{
menu = prompt("please select a number between 1 and 3",0);
menu = parseInt(menu)
//more code
}while(menu!=3);
//完整代码
function Menu()
{
var menu =0;
document.write(" menu options " + name + "<br>");
document.write("option 1 stringFunction<br>");
document.write("option 2 numberFunction<br>");
document.write("option 3 goodbye<br>");
do{
menu = prompt("please select a number between 1 and 3",0);
menu = parseInt(menu)
switch (menu)
{ // begin switch
case 1:
// begin case 1
document.write(name + " This is option 1<br> ") ;
stringFunction()
break ;
// end case 1
case 2:
// begin case 2
document.write(name + " This is option 2<br>") ;
numberFunction()
break ;
// end case 2
case 3:
// begin case 3
document.write('Goodbye ' + name) ;
break ;
// end case 3
default :
{// begin default
alert ("You must choose either 1,2,or 3");
}
}while(menu!=3);
} // end switch
} // end function
功能菜单()
{
var菜单=0;
文档。写入(“菜单选项”+名称+”
”;
编写(“选项1 stringFunction
”;
文件。写入(“选项2数字功能
”;
文件。写入(“选项3再见
”;
做{
菜单=提示(“请选择一个介于1和3之间的数字”,0);
菜单=parseInt(菜单)
开关(菜单)
{//开始开关
案例1:
//开始案例1
文件。写入(名称+“这是选项1
”;
stringFunction()
打破
//结案1
案例2:
//开始案例2
文件。写入(名称+“这是选项2
”;
numberFunction()
打破
//结束情况2
案例3:
//开始案例3
文件。写(‘再见’+姓名);
打破
//结束情况3
违约:
{//开始默认设置
警报(“您必须选择1、2或3”);
}
}while(菜单!=3);
}//结束开关
}//结束函数
这可能是您的解决方案:这可能是您的解决方案: