【发布时间】:2014-10-07 18:05:58
【问题描述】:
我在一段时间内嵌套了一个 for 循环来跟踪时间。如果满足某个条件,while 循环会跟踪反弹。只要满足该条件,for 循环就会继续计数。但是一旦满足条件,循环就会停止。但是,无论 while 循环内的条件如何,它都会继续。
/*
* @Author Lawton C Mizel
* @Version 1.0, 07 October 2014
*
* A program that simulates a ball bouncing by computing
* its height in feet and each "second" as time passes on
* a simulated clock.
*
*/
public class bouncyballs001 {
public static void main(String[] args) {
// create and connect scanner object
Scanner keyboard = new Scanner(System.in);
//introduce program
System.out.println("Welcome to the bouncing ball program!");
//prompts the user
System.out.println("Please enter the initial velocity: ");
double vel = keyboard.nextInt();
//initial variables
double height = 0;
int bounce = 0;
while (bounce < 5) {
for (int time = 0; time <= 30; time++) //counter
{
if (time >= 0) {
height = height + vel;
vel = vel - 32.0;
}
if (height < 0) {
height = height * -0.5;
vel = vel * -0.5;
System.out.println("BOUNCE!");
bounce++;
}
System.out.println("time: " + time + " " + "height: " + height);
}
}
}
}
【问题讨论】:
标签: java loops for-loop while-loop