如何在 switch 语句中显示用户输入?
how to display user input in a switch statement?
我目前正忙于用户列表挑战,这就是挑战:
-向数据库添加 3 个新用户(通过用户输入获取,而不是硬编码)
- 通过索引从数据库中删除用户。 (用户输入,无硬编码)
-concatenate/merge 将数据库中的所有用户名合并为一个字符串并在-the DOM 中适当显示
- 按字母顺序对数据库中的用户名进行排序并将其注销到控制台
这是我的代码:
let userlist = ["Steven", "Michael","Sarah","Lisa","Tamryn", "Wayne","Kirsten", "Peter", "Gwen", "Tamzin"];
console.log(userlist);
let menu = (prompt("=====Menu=====\n 1.Add three users\n 2.Remove User\n 3.Display All users\n 4.Sort users\n")-1);
switch(userlist)
{
case 0:
let newUsers = prompt("Add three user");
userlist.push(newUsers);
break;
case 1:
let removeUser = prompt("Remove a User");
newUsers = userlist.indexOf(removeUser);
let removedUser = userlist.splice(newUsers,1);
console.log(indexRemoved + " " + removedUser);
alert(`${removedUser} has been removed from the database`);
break;
case 2:
let display = prompt("Display a User");
break;
case 3:
let sort = prompt("Sort users")
break;
default:
alert("Please select 1 - 4 to choose any of the options displayed.");
break;
}
我不确定如何实现后两种情况
我已经更正了您程序中的 一些 错误,但您编写的某些功能中仍有一些错误需要更正。这只是为了为您的应用程序提供一个工作框架。
主要变化:
- 您需要将输入解析为
number
否则您的开关将无法正常工作
- 您需要打开
menu
而不是 userlist
的解析值,因为 userlist
是一个数组,您正试图将其与 number
进行比较你的switch
声明
- 我在您的应用程序中添加了一个无限循环,这意味着用户总是会在某些选项被 select 编辑后返回到菜单,并且可以 select 他/她的下一件事想做。为了能够退出这个循环,我添加了一个 Exit 选项来结束应用程序。
您现在可以运行申请。添加用户以及显示用户应该可以工作。
let userlist = [
"Steven",
"Michael",
"Sarah",
"Lisa",
"Tamryn",
"Wayne",
"Kirsten",
"Peter",
"Gwen",
"Tamzin",
];
console.log(userlist);
// make program run indefinitely (until tab/ browser is closed or Exit option is selected)
let exit = false;
while (true) {
let menu = prompt(
"=====Menu=====\n 1. Add an user\n 2. Remove user\n 3. Display all users\n 4. Sort users\n 5. Exit\n"
)
// Parse result as Integer
// you did it with -1, but it would be more natural to just parse the number as displayed on the screen and adjust the values in the switch accordingly
menu = Number.parseInt(menu) - 1;
if(isNaN(menu)){
// something else than a number was entered
alert("You must enter a number!")
// move on to next iteration => means here: display menu again.
continue;
}
// now use menu to determine which option was choosen
switch (menu) {
case 0:
// added an instruction so a user knows what to do
let newUsers = prompt("Add a user: \nPlease provide a username");
userlist.push(newUsers);
break;
case 1:
// added an instruction so a user knows what to do
let removeUser = prompt(
"==Remove a User==\nPlease provide the hame of the user you want to delete"
);
newUsers = userlist.indexOf(removeUser);
let removedUser = userlist.splice(newUsers, 1);
console.log(indexRemoved + " " + removedUser);
alert(`${removedUser} has been removed from the database`);
break;
case 2:
alert(`Display all users:\n ${userlist}`);
break;
case 3:
let sort = prompt("Sort users");
// todo
break;
case 4:
exit = true;
break;
default:
alert("Please select 1 - 4 to choose any of the options displayed.");
break;
}
// if user wants to exit the program break the infinite loop
if(exit){
exit = false;
break;
}
}
您的结果存储在 menu
变量中,因此您的开关应该比较此变量,而不是 userlist
。
您只需按原样进行切换 =>
switch(menu)
我目前正忙于用户列表挑战,这就是挑战:
-向数据库添加 3 个新用户(通过用户输入获取,而不是硬编码) - 通过索引从数据库中删除用户。 (用户输入,无硬编码) -concatenate/merge 将数据库中的所有用户名合并为一个字符串并在-the DOM 中适当显示 - 按字母顺序对数据库中的用户名进行排序并将其注销到控制台
这是我的代码:
let userlist = ["Steven", "Michael","Sarah","Lisa","Tamryn", "Wayne","Kirsten", "Peter", "Gwen", "Tamzin"];
console.log(userlist);
let menu = (prompt("=====Menu=====\n 1.Add three users\n 2.Remove User\n 3.Display All users\n 4.Sort users\n")-1);
switch(userlist)
{
case 0:
let newUsers = prompt("Add three user");
userlist.push(newUsers);
break;
case 1:
let removeUser = prompt("Remove a User");
newUsers = userlist.indexOf(removeUser);
let removedUser = userlist.splice(newUsers,1);
console.log(indexRemoved + " " + removedUser);
alert(`${removedUser} has been removed from the database`);
break;
case 2:
let display = prompt("Display a User");
break;
case 3:
let sort = prompt("Sort users")
break;
default:
alert("Please select 1 - 4 to choose any of the options displayed.");
break;
}
我不确定如何实现后两种情况
我已经更正了您程序中的 一些 错误,但您编写的某些功能中仍有一些错误需要更正。这只是为了为您的应用程序提供一个工作框架。
主要变化:
- 您需要将输入解析为
number
否则您的开关将无法正常工作 - 您需要打开
menu
而不是userlist
的解析值,因为userlist
是一个数组,您正试图将其与number
进行比较你的switch
声明 - 我在您的应用程序中添加了一个无限循环,这意味着用户总是会在某些选项被 select 编辑后返回到菜单,并且可以 select 他/她的下一件事想做。为了能够退出这个循环,我添加了一个 Exit 选项来结束应用程序。
您现在可以运行申请。添加用户以及显示用户应该可以工作。
let userlist = [
"Steven",
"Michael",
"Sarah",
"Lisa",
"Tamryn",
"Wayne",
"Kirsten",
"Peter",
"Gwen",
"Tamzin",
];
console.log(userlist);
// make program run indefinitely (until tab/ browser is closed or Exit option is selected)
let exit = false;
while (true) {
let menu = prompt(
"=====Menu=====\n 1. Add an user\n 2. Remove user\n 3. Display all users\n 4. Sort users\n 5. Exit\n"
)
// Parse result as Integer
// you did it with -1, but it would be more natural to just parse the number as displayed on the screen and adjust the values in the switch accordingly
menu = Number.parseInt(menu) - 1;
if(isNaN(menu)){
// something else than a number was entered
alert("You must enter a number!")
// move on to next iteration => means here: display menu again.
continue;
}
// now use menu to determine which option was choosen
switch (menu) {
case 0:
// added an instruction so a user knows what to do
let newUsers = prompt("Add a user: \nPlease provide a username");
userlist.push(newUsers);
break;
case 1:
// added an instruction so a user knows what to do
let removeUser = prompt(
"==Remove a User==\nPlease provide the hame of the user you want to delete"
);
newUsers = userlist.indexOf(removeUser);
let removedUser = userlist.splice(newUsers, 1);
console.log(indexRemoved + " " + removedUser);
alert(`${removedUser} has been removed from the database`);
break;
case 2:
alert(`Display all users:\n ${userlist}`);
break;
case 3:
let sort = prompt("Sort users");
// todo
break;
case 4:
exit = true;
break;
default:
alert("Please select 1 - 4 to choose any of the options displayed.");
break;
}
// if user wants to exit the program break the infinite loop
if(exit){
exit = false;
break;
}
}
您的结果存储在 menu
变量中,因此您的开关应该比较此变量,而不是 userlist
。
您只需按原样进行切换 =>
switch(menu)