JavaScript项目
我正在写一个JavaScript页面,下面是我的说明:(代码如下)JavaScript项目,javascript,Javascript,我正在写一个JavaScript页面,下面是我的说明:(代码如下) 创建一个名为color的变量。将提示对话框的结果指定为颜色变量的值 提示对话框要求用户选择红色、白色或蓝色 创建switch语句时,测试表达式是转换为大写的颜色变量 在大小写关键字后使用红色、白色、蓝色 在代码执行时,向用户反映所选的选项 添加一个默认子句,如果用户输入的选项不是提供的选项,则将执行该子句 请记住,用户可以在提示对话框中单击“取消”。在switch语句之前创建代码以处理这种情况 这是我的密码: <!DO
- 创建一个名为color的变量。将提示对话框的结果指定为颜色变量的值
- 提示对话框要求用户选择红色、白色或蓝色
- 创建switch语句时,测试表达式是转换为大写的颜色变量
- 在大小写关键字后使用红色、白色、蓝色
- 在代码执行时,向用户反映所选的选项
- 添加一个默认子句,如果用户输入的选项不是提供的选项,则将执行该子句
- 请记住,用户可以在提示对话框中单击“取消”。在switch语句之前创建代码以处理这种情况
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Project 4 4-29</title>
<script type="text/javascript">
var color;
color = prompt("Enter a color of either red, white, or blue");
switch (color){
case "red":
alert("RED");
break;
case "white:
alert("WHITE");
break;
case "blue":
alert("BLUE");
}
default: prompt("You have entered a color that is not one of the choices");
</script>
</html>
项目4 4-29
颜色变异;
颜色=提示(“输入红色、白色或蓝色”);
开关(彩色){
案例“红色”:
警报(“红色”);
打破
案例“白色:
警惕(“白色”);
打破
案例“蓝色”:
警报(“蓝色”);
}
默认值:提示(“您输入的颜色不是选项之一”);
关于我做错了什么,有什么建议吗?请记住,我是新手,所以请简单一点!您有以下语法错误:
switch (color){
case "red":
...
default:
alert("Default");
}
case "white"://<-- you are also missing the closing double quotes.
alert("WHITE");
break;
case "blue":
alert("BLUE");
break;//<-- use break to prevent fallback
default: //<-- put inside switch ... case
alert("You have entered a color that is not one of the choices");
}
case“white”://您的代码中有许多问题。
未关闭。在第二个案例中,case“white:
应该是
case "white":
行default:prompt(“您输入的颜色不是选项之一”)代码>应该在开关{}内
所以你可以试试这个
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Project 4 4-29</title>
<script type="text/javascript">
var color;
color = prompt("Enter a color of either red, white, or blue");
switch (color){
case"red":
alert("RED");
break;
case "white":
alert("WHITE");
break;
case "blue":
alert("BLUE");
break;
default:
prompt("You have entered a color that is not one of the choices");
}
</script>
<head>
<body>
</body>
</html>
项目4 4-29
颜色变异;
颜色=提示(“输入红色、白色或蓝色”);
开关(彩色){
案例“红色”:
警报(“红色”);
打破
“白色”案例:
警惕(“白色”);
打破
案例“蓝色”:
警报(“蓝色”);
打破
违约:
提示(“您输入的颜色不是选项之一”);
}
您可以使用while
循环来确保用户最终输入有效的颜色
JS:
这里有一个。我喜欢这个选项并尝试了它,但它不会在浏览器(Chrome)中呈现。注意,如果没有中断
在blue
案例中,输入blue将再次提示用户输入颜色。虽然这可能会回答OP的问题,但几句解释将帮助当前和未来的读者更好地理解此答案。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Project 4 4-29</title>
<script type="text/javascript">
var color;
color = prompt("Enter a color of either red, white, or blue");
switch (color){
case"red":
alert("RED");
break;
case "white":
alert("WHITE");
break;
case "blue":
alert("BLUE");
break;
default:
prompt("You have entered a color that is not one of the choices");
}
</script>
<head>
<body>
</body>
</html>
var choosing = true;
var color = prompt('Please choose a color: red, white, or blue');
while (choosing) {
switch (color) {
case 'red':
alert('RED');
choosing = false;
break;
case 'white':
alert('WHITE');
choosing = false;
break;
case 'blue':
alert('BLUE');
choosing = false;
break;
default:
color = prompt('You didn\'t enter a valid color. Please choose a color: red, white, or blue');
}
}