Home >Java >javaTutorial >A classic Java programming question

A classic Java programming question

怪我咯
怪我咯Original
2017-06-26 11:53:281155browse

A ball falls freely from a height of 100 meters. Each time it hits the ground, it bounces back to half of its original height. It falls again... How many meters does it pass when it hits the ground for the 10th time? How high is the 10th rally?

public class Example10 {
    public static void main(String[] args) {
        height();
    }
    public static void height() {
        double sum = 0;
        double h = 100;
        for (int i = 0; i < 10; i++) {
            sum += h;
            h = h / 2;
        }
        System.out.println("共经过"+sum+"米。第10次反弹"+h+"米。");
    }
}


The above is the detailed content of A classic Java programming question. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn