java - 遥不可及的声明,永无止境的倒计时

标签 java oop

我试图完成这个程序,它告诉我在我的主要方法中,fallItem.trackingMethod(); 是一个无法访问的语句,我无法弄清楚为什么......

此外,我的倒数计时器应该在 0 处停止,但只是变为负值,直到我在程序上点击停止才会停止。

感谢任何指点,我已经学习 Java 大约 5 个星期了......我必须在这里遗漏一些小细节......

倒计时部分:

System.out.println("Countdown");
for (int i = 5; 1 >= 1; i--) {
    System.out.println(i);
}

MyTrajectoryProjector 类:

public class MyTrajectoryProjector {
    public static double HEIGHT_THRES = 600;

    public static double startingPosition() {
        Scanner keyboard = new Scanner(System.in);

        double aPosition;

        do {
            System.out.printf("\n\nEnter the initial position (must be over 600.0 feet): ");
            aPosition = keyboard.nextDouble();
            if ((aPosition <= HEIGHT_THRES)) {
                System.out.printf("Error - position too low. Try again.");
            }
        }
        while ((aPosition <= HEIGHT_THRES));

        return aPosition;
    }

    public static double startingVelocity() {

        Scanner keyboard = new Scanner(System.in);

        double aVelocity;  //user entered position

        do {
            System.out.printf("\n\nEnter the initial velocity (-500.0 ft/sec or more): ");
            aVelocity = keyboard.nextDouble();


            if ((aVelocity <= MyFallingItem.TERM_VELOC)) {
                System.out.printf("Error - velocity too low. Try again.");
            }
        }
        while ((aVelocity <= MyFallingItem.TERM_VELOC));

        return aVelocity;
    }

    public static void main(String[] args) {
        System.out.print("This program will calculate the position and velocity "
                + "of a falling object \nuntil it reaches " + HEIGHT_THRES
                + " feet above ground.");

        double aPosition = startingPosition();
        double aVelocity = startingVelocity();

        MyFallingItem fallItem = new MyFallingItem(aPosition, aVelocity);

        System.out.printf("\n\n");

        System.out.println("Countdown");
        for (int i = 5; 1 >= 1; i--) {
            System.out.println(i);
        }

        fallItem.trackingMethod();
        System.out.printf("object reached " + HEIGHT_THRES + " feet after "
                + fallItem.getTimeNow() + " seconds. \n The object's final "
                + "position is " + fallItem.getPosNow() + " feet.");
    }

}

MyFallingItem 类:

public class MyFallingItem {
    private final double INI_POS;    //needs to be a constant
    private final double INI_VEL;    //needs to be a constant
    private int timeNow;    //current time
    private double posNow;   //current position
    private double velNow;   //current velocity
    public static double TERM_VELOC = -500;   //terminal velocity (-500 feet/sec)

    //MyFallingItem constructor
    public MyFallingItem(double aPosition, double aVelocity) {
        this.INI_POS = aPosition;
        this.posNow = 0.0;
        this.INI_VEL = aVelocity;
        this.velNow = 0.0;
        this.timeNow = 0;
    }

    public int getTimeNow() {
        return timeNow;
    }

    public double getPosNow() {
        return posNow;
    }

    public void updateMethod() {
        timeNow++;

        //V(t) = –32t + V0 (current velocity)
        velNow = -32 * timeNow + 0.0;
        if (velNow < TERM_VELOC) {
            velNow = TERM_VELOC;
        }


        if (velNow > TERM_VELOC) {
            posNow = +500;
        } else {
            //P(t) = –16t2 + V0t + H0 (current position)
            posNow = Math.pow(-16 * timeNow, 2) + INI_VEL * timeNow + INI_POS;
        }

    }

    public void trackingMethod() {
        System.out.printf("The initial position is " + INI_POS);
        System.out.printf("The initial velocity is " + INI_VEL);

        this.updateMethod();
        while (this.posNow >= MyTrajectoryProjector.HEIGHT_THRES) {
            System.out.printf("Object released from %.1f" + this.INI_POS + " feet at an "
                    + "initial velocity of %.1f" + this.INI_VEL + " ft/sec");
            this.updateMethod();

            System.out.printf("    at " + this.timeNow + " seconds, position is %.1f"
                    + posNow + " and velocity is %.1f" + this.velNow + " ft/sec");

        }
    }

}

最佳答案

替换

for (int i = 5; 1 >= 1; i--)

与:

for (int i = 5; i >= 1; i--)

for 循环中的第二个参数是必需条件。您定义的条件是:1 >= 1 始终为真。

您忘记更改条件以检查变量 i 的值,因此,您进入了一个无限循环,这可不好!


Quick reference about for loops in java.

关于java - 遥不可及的声明,永无止境的倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35399404/

相关文章:

java - SpringBoot 从 2.4.X 升级到 2.6.X 后无法运行我的 jar

java - 找到最少的列数

Java HashMap 数组大小

在类的所有方法中将参数转换为相同标准的 Pythonic 方式

oop - 类属性声明: private vs public

java - 如何在java中锁定Stage的大小

java - 如何在 jpopupmenu 中捕获用户对 jradiobutton 的选择?

java - 为什么不能列出 <? extends List<T>> 被分配给 List<List<T>>

actionscript-3 - 关于带接口(interface)的类实现问题

javascript - TypeScript项目组织,编译成单个JS文件?