Java 为什么';我的弹跳球计划不会在第五次弹跳后停止吗?

Java 为什么';我的弹跳球计划不会在第五次弹跳后停止吗?,java,loops,for-loop,while-loop,Java,Loops,For Loop,While Loop,我在一段时间内嵌套了一个for循环来跟踪时间。如果满足某个条件,while循环将跟踪反弹。只要满足该条件,for循环将继续计数。但一旦满足条件,循环就应该停止。但是,无论while循环中的条件如何,它都将继续 /* * @Author Lawton C Mizel * @Version 1.0, 07 October 2014 * * A program that simulates a ball bouncing by computing

我在一段时间内嵌套了一个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);
            }
        }
    }
}
    for(int time=0; time <= 30 && bounce < 5; time++) //counter, bails out if bounce > 5
    {
        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);
    }
/*
*@作者劳顿·米泽尔
*@Version 1.0,2014年10月7日
* 
*通过计算模拟球反弹的程序
*它的高度以英尺为单位,随着时间的推移每“秒”一次
*模拟时钟。
* 
*/
公共类弹跳球S001{
公共静态void main(字符串[]args){
//创建并连接扫描仪对象
扫描仪键盘=新扫描仪(System.in);
//介绍节目
System.out.println(“欢迎来到弹跳球计划!”);
//提示用户
System.out.println(“请输入初始速度:”);
double-vel=keyboard.nextInt();
//初始变量
双倍高度=0;
int bounce=0;
同时(反弹<5){
for(int time=0;time=0){
高度=高度+水平;
vel=vel-32.0;
}
如果(高度<0){
高度=高度*-0.5;
vel=vel*-0.5;
System.out.println(“BOUNCE!”);
反弹++;
}
System.out.println(“时间:“+时间+”+“高度:“+高度”);
}
}
}
}

您在一个永远不会被调用的条件中拥有bounce++

if (height < 0)
if(高度<0)
将永远不会为真,因为高度从0开始并上升(即,永远不会为负)


这意味着bounce永远只能是0。

在一个永远不会被调用的条件中有bounce++

if (height < 0)
if(高度<0)
将永远不会为真,因为高度从0开始并上升(即,永远不会为负)


这意味着反弹将永远是0。

除非时间增加30倍,否则无法到达外部while循环。您可以将反弹要求添加到for循环并删除while循环。现在的情况是,在外部while循环中检查反弹之前,可以在for循环中反弹30次

/*
     * @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);
            }
        }
    }
}
    for(int time=0; time <= 30 && bounce < 5; time++) //counter, bails out if bounce > 5
    {
        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);
    }
for(int-time=0;time 5
{
如果(时间>=0)
{
高度=高度+水平;
vel=vel-32.0;
}
如果(高度<0)
{
高度=高度*-0.5;
vel=vel*-0.5;
System.out.println(“BOUNCE!”);
反弹++;
}
System.out.println(“时间:“+时间+”+“高度:“+高度”);
}

或者,您可以使用if语句和
break
直到时间增加30倍,您才到达外部while循环。您可以将反弹要求添加到for循环并删除while循环。发生的情况是,在外部while循环中选中反弹之前,您可以在for循环中反弹30次

/*
     * @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);
            }
        }
    }
}
    for(int time=0; time <= 30 && bounce < 5; time++) //counter, bails out if bounce > 5
    {
        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);
    }
for(int-time=0;time 5
{
如果(时间>=0)
{
高度=高度+水平;
vel=vel-32.0;
}
如果(高度<0)
{
高度=高度*-0.5;
vel=vel*-0.5;
System.out.println(“BOUNCE!”);
反弹++;
}
System.out.println(“时间:“+时间+”+“高度:“+高度”);
}

或者,你可以使用if语句和
break

,但是vel也可以是负数,所以高度也可以是负数,不是吗?速度可以是负数,但是我假设高度不会是负数,因为这意味着它在地底下,因此这个if语句永远不会达到。高度可以是负数。尝试使用16作为速度和高度通过4个循环。我假设我最初的假设是比较高度的起始参考点是球反弹的地面(高度=0).但是vel也可以是负数,所以高度也可以是负数,不是吗?速度可以是负数,但是我假设高度不会是负数,因为这意味着它在地下,因此这个if语句永远不会达到。高度可以是负数。试着用16作为速度,运行4个循环。我想我最初的假设是开始g比较高度的参考点是球弹跳的地面(高度=0)。