Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 遥不可及的陈述,永不停止的倒计时_Java_Oop - Fatal编程技术网

Java 遥不可及的陈述,永不停止的倒计时

Java 遥不可及的陈述,永不停止的倒计时,java,oop,Java,Oop,我试图完成这个程序,它告诉我在我的主方法中,fallItem.trackingMethod()是一个无法访问的语句,我不明白为什么 此外,我的倒计时应该在0停止,但只是变为负数,直到我按下程序上的“停止”按钮才会停止 非常感谢您的指点,我已经学习java大约5周了。。。我必须在这里遗漏一些小细节 倒计时部分: System.out.println("Countdown"); for (int i = 5; 1 >= 1; i--) { System.out.println(i);

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

此外,我的倒计时应该在0停止,但只是变为负数,直到我按下程序上的“停止”按钮才会停止

非常感谢您的指点,我已经学习java大约5周了。。。我必须在这里遗漏一些小细节

倒计时部分:

System.out.println("Countdown");
for (int i = 5; 1 >= 1; i--) {
    System.out.println(i);
}
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.");
    }

}
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");

        }
    }

}
MyTrajectoryProjector类:

System.out.println("Countdown");
for (int i = 5; 1 >= 1; i--) {
    System.out.println(i);
}
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.");
    }

}
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");

        }
    }

}
公共类MyTrajectoryProjector{
公共静态双倍高度=600;
公共静态双启动位置(){
扫描仪键盘=新扫描仪(System.in);
双移位;
做{
System.out.printf(“\n\n输入初始位置(必须超过600.0英尺):”;
aPosition=keyboard.nextDouble();
如果((位置更换

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


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

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


更换

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


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

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


@Evin1_u给了您正确的代码(编辑:现在添加了一个解释),但我想提供一个解释

他是对的:
for(inti=5;1>=1;i-)
是坏代码,但原因如下。控制循环运行多长时间的表达式,
1>=1
,始终是真的。因为你有这个,你基本上是在写一个循环,在最后递减
i
,这就是为什么

[它]只是转为负值,直到我点击停止程序才会停止

@Evin1_uu为您提供了正确的代码(编辑:现在添加了一个解释),但我想提供一个解释

他是对的:
for(inti=5;1>=1;i-)
是坏代码,但原因如下。控制循环运行多长时间的表达式,
1>=1
,始终是真的。因为你有这个,你基本上是在写一个循环,在最后递减
i
,这就是为什么

[它]只是转为负值,直到我点击停止程序才会停止


我花了一段时间才注意到这个
1>=1
。我正要评论这两行是一样的:)哇,我知道这很简单……因为来自地狱的永无止境的循环,它永远无法到达
fallItem.trackingMethod()
我花了一段时间才注意到这个
1>=1
。我正要评论这两行是一样的:)哇,我知道这很简单……因为地狱里的无止境循环,它永远无法到达
fallItem.trackingMethod()
我明白了,谢谢你们提供的信息,你们的详细程度还是令人惊讶的。我一直盯着这个,好像永远都看不见了it@Newb2Java我,j,l,1是麻烦的来源我明白了,谢谢你们提供的信息,你们的详细程度还是令人惊讶的。我一直盯着这个看,好像是永远和d看不见it@Newb2Javai,j,l,1是麻烦的来源