第二练

JavaScript 编程题

下面两个函数的返回值是一样的吗?为什么?

function foo1() {
    return {
        bar: "hello"
    };
}

function foo2() {
    return
    {
        bar: "hello"
    };
}

在编程语言中,基本都是使用分号(;)将语句分隔开,这可以增加代码的可读性和整洁性。而在 JS 中,如若语句各占独立一行,通常可以省略语句间的分号(;),JS 解析器会根据能否正常编译来决定是否自动填充分号:

var test = 1 + 2;
console.log(test); //3

在上述情况中,为了正确解析代码,就不会自动填充分号了,但是对于 return 、break、continue 等语句,如果后面紧跟换行,解析器一定会自动在后面填充分号(;),所以上面的第二个函数就变成了这样:

function foo2() {
    return;
    {
        bar: "hello"
    };
}

所以第二个函数是返回 undefined。


MySQL 编程题

用一条 SQL 语句,查询出每门课都大于 80 分的学生姓名。

表名 student_score

name course score
张三 语文 81
张三 数学 75
李四 语文 76
李四 数学 90
王五 语文 81
王五 数学 100
王五 英语 90
SELECT DISTINCT name FROM student_score
WHERE name NOT IN (SELECT DISTINCT name FROM student_score WHERE score<=80);

或者

SELECT name FROM student_score GROUP BY name HAVING MIN(score)>80;

Java 编程题

一球从 100 米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第 10 次落地时,共经过多少米?第 10 次反弹多高?

package test;

public class Test2 {
    /**
     * 
     * @param h 距离
     * @param n 次数
     * @return 第n次反弹的高度
     */
    public static double sumBallHeight(double h, int n) {
        if (n == 1)
            return h / 2;
        else
            return sumBallHeight(h / 2, n - 1);
    }

    public static void main(String[] args) {
        System.out.println(sumBallHeight(100, 10));
    }
}

package test;

public class Tl2 {

    public static void sumBallHeight(int initHeight, int times) {
        // 落地时经过多少米
        double sum = 0;
        // 反弹高度
        double height = 0;
        for (int i = 1; i <= times; i++) {
            if (i == 1) {
                height  = initHeight/2;
                sum += initHeight;
            } else {
                sum = sum + height * 2;
                height = height/2;
            }
        }
        System.out.println("第" + times + "次落地时,共经过" + sum + "米");
        System.out.println("第" + times + "次反弹" + height + "米");
    }

    public static void main(String[] args) {
        sumBallHeight(100, 3);
    }
}

猜你喜欢

转载自www.cnblogs.com/czh518518/p/11273848.html