问答详情
源自:4-10 编程练习

这样子用if为什么输不出数据

var infos=[["小A","女","21","大一"],["小B","男","23","大三"],["小C","男","24","大四"],

    ["小D","女","21","大一"],["小E","男","23","大三"],["小F","女","24","大四"],["小G","男","23","大一"],

    ["小H","男","21","大一"],["小I","女","23","大四"],["小J","男","22","大二"]];

    for(var i=0;i<infos.length;i++)

    {

        for(var j=0;j<infos[i].length;j++)

        {

            if((infos[i][j]=="大一" )&&(infos[i][j]=="女"))

            {

                document.write("大一女生:"+infos[i]+"<br>");

                

            }

            

        }

    }

   


提问者:慕姐7545903 2017-11-20 10:22

个回答

  • 彼岸花开灬忆流年
    2017-11-20 11:13:18
    已采纳

    你的j不可能同时取两个值,假如匹配["小A","女","21","大一"],你的  if((infos[0][3]=="大一" )&&(infos[0][1]=="女")),j值怎么可能在一次循环中即取到3又取到1呢,可以这样

    var infos=[["小A","女","21","大一"],["小B","男","23","大三"],["小C","男","24","大四"],
        ["小D","女","21","大一"],["小E","男","23","大三"],["小F","女","24","大四"],["小G","男","23","大一"],
        ["小H","男","21","大一"],["小I","女","23","大四"],["小J","男","22","大二"]];
        for(var i=0;i<infos.length;i++)  {
            for(var j=0;j<infos[i].length;j++)  {
                if(infos[i][j]=="大一" ) {     //匹配到大一学生
                           if(infos[i][1]=="女"){     //再匹配大一学生中的女生
                          document.write("大一女生:"+infos[i]+"<br />");
                 }
              }
            }
        }

  • 沐沐幕
    2018-01-13 16:59:05

    ···