为了账号安全,请及时绑定邮箱和手机立即绑定

将变量设置回零

将变量设置回零

拉丁的传说 2023-05-24 17:32:41
我正在使用亚马逊上的一本电子书自学 Java。我正在上一门对计算机进行“基准测试”的课程。它通过循环一分钟并计算结果来完成此操作。在完成之前,它基本上不会在一分钟内向您显示任何内容。所以我做了一个小修改,每隔几秒显示一个点作为某种进度条。通常这是一件微不足道的事情,但有些地方不对劲,我不知道是什么。发生的事情是 miniIndex 将达到我指定的阈值,并打印 miniIndex 的值和一个句点。然后应该将 miniIndex 设置为零,以便计数器可以重新启动。但它不会重置,也不会再增加。这是非常奇怪的行为。这是完整的代码:class Benchmark {    public static void main(String[] arguments) {        long startTime = System.currentTimeMillis();        long endTime = startTime + 60000;        long index = 0;        // My inner index        int miniIndex = 0;        //        while (true) {            double x = Math.sqrt(index);            long now = System.currentTimeMillis();            if (now > endTime){                break;            }            index++;            // my modification            miniIndex++;            if (miniIndex >= 5000) {                System.out.print(miniIndex + ".");                miniIndex = 0;            }            // end of my modification        }        System.out.println(index + " loops in one minute.");    }}
查看完整描述

3 回答

?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

我认为您误解了您的操作在做什么,因为它不是在计算毫秒,而是在计算彼此不miniIndex++相等的循环迭代次数。根据您想要发生的情况, 我已经修改了您的代码以每 5 秒执行一次语句:if


public static void main(String[] arguments) {


    long startTime = System.currentTimeMillis();

    long miniTime = startTime; //Declare miniTime equal to startTime

    long endTime = startTime + 60000;

    long index = 0;


    while (true) {

        double x = Math.sqrt(index);

        long now = System.currentTimeMillis();

        if (now > endTime){

            break;

        }

        index++;


        // my modification    

        //Current time minus last time the if executed and check if 5 seconds passed

        if ((now - miniTime) >= 5000) { 

            miniTime = System.currentTimeMillis();

            System.out.println("5 seconds have passed.");


            //if you want to print the actual time elapsed every 5 seconds use this print

            //System.out.println((now - startTime)/1000 + " seconds have passed.");

        }

        // end of my modification

    }

    System.out.println(index + " loops in one minute.");

}

请注意我现在如何比较当前时间now并减去miniTime以检查它是否大于或等于 5000 毫秒。要使用时间,您必须以某种方式将它与时间联系起来,在这种情况下System.currentTimeMillis()以及结果。数字本身(例如循环计数)永远不会是时间一致的。


一个循环可能执行数百万次,但只需要 3 秒。


示例输出:


5 seconds have passed.

5 seconds have passed.

5 seconds have passed.

5 seconds have passed.

5 seconds have passed.

5 seconds have passed.

5 seconds have passed.

5 seconds have passed.

5 seconds have passed.

5 seconds have passed.

5 seconds have passed.

16319642816 loops in one minute.

注意: 5 seconds have passed.打印 11 次,因为在 60 秒标记时循环被打破,所以最后一次打印没有打印。(11 * 5前 55 秒为 55)。


查看完整回答
反对 回复 2023-05-24
?
叮当猫咪

TA贡献1776条经验 获得超12个赞

您的代码运行良好;是你的期望有缺陷。

您的代码每 5,000 次迭代打印一个点。这基本上会产生价值。请记住,您的 CPU 每秒运行超过 20 亿次操作。您可能每秒可以执行几百万次这样的循环。称其为每秒一百万个循环,除以 5000 就是每秒 200 个点,或多或少。


查看完整回答
反对 回复 2023-05-24
?
倚天杖

TA贡献1828条经验 获得超3个赞

如果您仍然想在那种类型的输出中使用它,您可以使用StringBuilder. 它的编码是这样的:


StringBuilder stringBuilder = new StringBuilder();

并包含这样的循环:


if (miniIndex >= 5000) {

    stringBuilder.append(miniIndex).append(".");

    miniIndex = 0;

}


if (stringBuilder.length() >= 200) {

    System.out.println(stringBuilder);

    stringBuilder.setLength(0);

}

//img1.sycdn.imooc.com//646dd9fd0001464412610176.jpg

查看完整回答
反对 回复 2023-05-24
  • 3 回答
  • 0 关注
  • 135 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信