2013-04-23 80 views
0

我需要编写一个循环,每百万次迭代打印一次消息。我想让它运行10秒(时钟时间)以查看打印了多少条语句。每100万次迭代打印一次消息

我想我只是绑住自己在海里,现在虽然...

public class OneInAMillion{ 
    public static void main(String []args){ 
    long startTime = System.currentTimeMillis(); //time at start of execution 
    long endTime = startTime + 10000; //time at end of execution (10000 = 10 seconds) 
    int count = 1; 

    while(System.currentTimeMillis() < endTime) { //run this before 10 seconds is up 
     for (int i = 0; i < count; i++) { 
     if(i % 1000000 == 0) { 
      System.out.println("Iteration: " + count++); //print every millionth iteration 
     } 
     } 
    } 

    System.out.println("Time up!"); //print once loop finishes 
    } 
} 
+1

什么问题?有没有错误,或者它不工作?你有什么尝试? – CodeChimp 2013-04-23 10:57:31

+1

您只能在while循环中使用,并在里面增加i。 – BobTheBuilder 2013-04-23 10:58:19

回答

0

摆脱你for循环,你if和打印i,而不是计数之前做i++,你应该设置。

public class OneInAMillion{ 
    public static void main(String []args){ 
    long startTime = System.currentTimeMillis(); //time at start of execution 
    long endTime = startTime + 10000; //time at end of execution (10000 = 10 seconds) 
    //int count = 1; 
    int i = 0; 
    while(System.currentTimeMillis() < endTime) { //run this before 10 seconds is up 
     i++; 
     if(i % 1000000 == 0) { 
      System.out.println("Iteration: " + i); //print every millionth iteration 
     } 
    } 

    System.out.println("Time up!"); //print once loop finishes 
    } 
} 
+0

这个答案在代码示例中会更好。 – 2013-04-23 11:05:12

5

你在循环中有一个循环。想想每个循环的作用 - 第一个循环会持续10秒。另一个将简单地从0开始到1,并且在while循环的下一次迭代中,它将从0到2,然后从0到3等等。由于0%1000000为0,所以它也会在0(很多)时打印。

尝试将它们组合成一个单一循环。这可以通过摆脱while循环的,只具有for循环与while循环的条件,因为这样做:

public static void main(String[] args) { 
    long endTime = System.currentTimeMillis() + 10000; //time at end of execution (10000 = 10 seconds) 
    for (int i = 1; System.currentTimeMillis() < endTime; i++) { 
     if(i % 1000000 == 0) { 
      System.out.println("Iteration: " + i/1000000); //print every millionth iteration 
     } 
     } 
     System.out.println("Time up!"); //print once loop finishes 
    } 

注意count将永远是我/ 1000000,所以我摆脱它。

+0

这似乎是迄今为止最合乎逻辑的推理,但它仍不会打印“时间到了!”消息,并且每次运行代码时都会得到完全不同的结果。 – samwhocan 2013-04-23 11:24:20

+0

@samwhocan只需将它替换为while循环以及它内部的内容,而不是之前或之后的代码,它将工作=) – ddmps 2013-04-23 12:07:32

+0

感谢您的建议。仍然没有工作,但...打印“错误!”多次! long startTime = System.currentTimeMillis(); long endTime = startTime + 10000; int count = 1;对于(int i = 1; System.currentTimeMillis() samwhocan 2013-04-23 12:09:41

-1

您的for循环退出条件干扰您的while循环退出条件并阻止您在期望时对其进行评估。摆脱while循环:

public class OneInAMillion{ 
    public static void main(String []args){ 
    long startTime = System.currentTimeMillis(); //time at start of execution 
    long endTime = startTime + 10000; //time at end of execution (10000 = 10 seconds) 
    int count = 1; 

    for (int i = 0; i < count; i++) { 
     if(System.currentTimeMillis() >= endTime) { 
     break; 
     } 

     if(i % 1000000 == 0) { 
     System.out.println("Iteration: " + count++); //print every millionth iteration 
     } 
    } 


    System.out.println("Time up!"); //print once loop finishes 
    } 
} 
+0

您的整个for循环相当于if语句(0%1000000 == 0)。它只会迭代一次,这不是OP想要的。 – ddmps 2013-04-23 11:08:40

+0

是的,我有for循环的错误退出条件;你的回答就是我的目标。 :} – 2013-04-23 11:11:41