<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>流程控制语句</title>
<script type="text/javascript">
var infos=[
['小A','女',21,'大一'],
['小B','男',23,'大三'],
['小C','男',24,'大四'],
['小D','女',21,'大一'],
['小E','女',22,'大四'],
['小F','男',21,'大一'],
['小G','女',22,'大二'],
['小H','女',20,'大三'],
['小I','女',20,'大一'],
['小J','男',20,'大三']
]
//第一步把之前的数据写成一个数组的形式,定义变量为 infos
for(var i=0;i<10;i++){
while(infos[i][1]=='女'){
switch(infos[i][3]=='大一')
{
case '大一':
document.write(infos[i][0]);
case '大二':
document.write(infos[i][0]);
case '大三':
document.write(infos[i][0]);
case '大四':
document.write(infos[i][0]);
default:
document.write("没有大一的女生");
}
}
}
</script>
</head>
<body>
</body>
</html>
var infos=[
["小A","女",21,"大一"],
["小B","男",23,"大三"],
["小C","男",24,"大四"],
["小D","女",21,"大一"],
["小E","女",22,"大四"],
["小F","男",21,"大一"],
["小G","女",22,"大二"],
["小H","女",20,"大三"],
["小I","女",20,"大一"],
["小J","男",20,"大三"]
];
//第一步把之前的数据写成一个数组的形式,定义变量为 infos
for (var i=0;i<infos.length ;i++ ){
if(infos[i][1]=="女"){
switch(infos[i][3]){
case "大一":
document.write(infos[i][0]);break;
}
}
}
原文中 default后面的冒号是中文符号,应该改为英文符号--:
switch后面的判断句 infos[i][3]=='大一' 值的类型只可能是boolean类型(true和false)
字符串应该用""而不是''
var stu=[
['小A','女',21,'大一'],
['小B','男',23,'大三'],
['小C','男',24,'大四'],
['小D','女',21,'大一'],
['小E','女',22,'大四'],
['小F','男',21,'大一'],
['小G','女',22,'大二'],
['小H','女',20,'大三'],
['小I','女',20,'大一'],
['小J','男',20,'大三'],
]; //第一步把之前的数据写成一个数组的形式,定义变量为 infos
for(var i=0;i<10;i++){
if(stu[i][3]=='大一'&&stu[i][1]=='女'){
document.write('大一女生:'+stu[i][0]+'<br>')
}
}
你的while循环是个死循环,没有跳出,会一直执行,来个if不是好些么,只是条件判断....每个case貌似没有break,会全部执行呀.......