【问题标题】:Time Deley Loop BlueJ延时循环 BlueJ
【发布时间】:2017-05-11 11:50:23
【问题描述】:

java.until.cocurrent.TimeUnit.SECOND.sleep(12)-

有人可以纠正这个问题,因为它在系统中不起作用。它一直说期望类、接口或枚举。

谢谢!

【问题讨论】:

  • 你必须纠正整个事情。 1. until --> util 2. cocurrent--> concurrent 3. SECOND --> SECONDS.
  • java.util.concurrent.TimeUnit.SECONDS.sleep(12);公共类 Comp_Project {public static void main() { System.out.println("A"); System.out.println("B"); } } 它仍然不工作
  • @NiyathiMeghana 请不要在评论中复制粘贴代码。使用编辑选项。 sleep() 应该在 main() 方法内而不是在类声明之上。
  • @RajeevSreedharan 我不明白。可以举个例子吗?
  • 我不会将此作为答案发布,这是更多的错字。你可以这样使用: public class Comp_Project {public static void main() { java.util.concurrent.TimeUnit.SECONDS.sleep(12); System.out.println("A"); System.out.println("B"); } }

标签: java bluej


【解决方案1】:

如果您只想让程序在打印字符串“A”和“B”之间“休眠”,您应该尝试以下方法(这里有两种可能的实现,都需要导入 java.util)。

import java.util.*; //Double-check if you have imported this to your project


//1st code option
public class Comp_Project{
    public static void main() {

        System.out.println("A");

        try{
            java.util.concurrent.TimeUnit.SECONDS.sleep(12);
            //try to sleep before printing "B"
        }
        catch(InterruptedException e){
            System.out.println(e); //prints exception if something goes wrong
        }  

        System.out.println("B");
    }
}

//2nd code option
public class Comp_Project extends Thread {
    public static void main() {

        System.out.println("A");

        try{
            Thread.sleep(12); //try to sleep before printing "B"
        }
        catch(InterruptedException e){
            System.out.println(e); //prints exception if something goes wrong
        }  

        System.out.println("B");
    }
}

【讨论】:

  • 非常感谢!
  • 不客气!如果可行,请不要忘记接受我的正确答案:)
  • 有没有办法在延时后打印B代替A?
  • 我没有得到你所需要的,所以我有三个建议。: 1- 交换打印行,所以 B 将在 A 之前打印。 2- 通过添加另一个在 B 之后再次打印 A B 之后的 println 命令。 3- 在 while 命令中添加整个代码(println+try+catch+println),这样它将循环直到达到所需的条件。
  • 我的意思是-程序打印'A',然后播放时间延迟。时间延迟后,“A”消失,“B”被打印在它的位置。
猜你喜欢
  • 1970-01-01
  • 2012-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-18
  • 1970-01-01
  • 1970-01-01
  • 2016-08-18
相关资源
最近更新 更多