问答详情
源自:7-23 编程练习

这个代码写的好啊(借鉴)

尤其是这句 score[i] = parseInt(mystr[i].split(":")[1]);很喜欢

提问者:weibo_刺眼的光芒闭目可视_0 2015-08-07 17:31

个回答

  • lt_mk
    2015-08-10 20:05:45

    1、字符串scoreStr被分裂成数组mystr=["小明:87","小花:81","小红:97","小天:76","小张:74","小小:94","小西:90","小伍:76","小迪:64","小曼:76"]
    2、第一次循环时,i=0,
    mystr[0]="小明:87",
    mystr[0].split(":")=["小明","87"]是一个数组,
    mystr[0].split(":")[0] ="小明",mystr[0].split(":")[1] ="87"
    而你说的mystr[0].split(":",1)只取第一个字符串,所以这个数组的长度为1,mystr[0].split(":",1)="小明"
    3、所以是我错了~不好意思啊,误导你了~赶紧改正@刘荣轩1991

  • lt_mk
    2015-08-10 14:57:22

    1、字符串scoreStr被分裂成数组mystr=["小明:87","小花:81","小红:97","小天:76","小张:74","小小:94","小西:90","小伍:76","小迪:64","小曼:76"]
    2、第一次循环时,i=1,
    mystr[1]="小明:87",
    mystr[1].split(":")=["小明","87"]是一个数组,
    mystr[1].split(":")[1] ="小明",mystr[1].split(":")[1] ="小明",mystr[1].split(":")[2] ="87"
    而你说的mystr[1].split(":",1)只取第一个字符串,所以这个数组的长度为1,mystr[1].split(":",1)="小明"
    3、所以这里应该改为mystr[i].split(":")[2]

  • lt_mk
    2015-08-10 13:51:14

    求平均值不应该是Math.round(sum /(i+1))么?

  • 刘荣轩1991
    2015-08-09 19:12:58

    split(“:”)【1】什么意思?为什么不用split(“:”,1)