而(var!= var)

时间:2010-01-15 12:24:01

标签: java

while(var!= var)     System.out.println(“loop ..”);

执行它.. 如何声明..var

4 个答案:

答案 0 :(得分:21)

假设这是家庭作业,我会给你一个提示,而不是答案。您需要像这样定义var

double var = // time for you to understand floating point

编辑:为了使这个答案更有用,这里是Wikipedia's entry on special floating-point values

答案 1 :(得分:15)

public static void main(String[] args) {
    double var = Double.NaN;
    System.out.println("var : " + var);
    while (var != var) {
        System.out.println(" I am inside loop now! ");
    }

}

答案 2 :(得分:4)

double var = Double.NaN

答案 3 :(得分:0)

此外,以下将导致无限循环,而不是无限循环;但是如果在主循环中有更多东西在进行,那么[我认为]这可能会有效地无限;我认为值得发布,因为这是现实生活中App Server可能发生的事情:

public class Puzzle extends Thread {
static boolean keepRunning=true;
static int var=0;



public void run() {

while (keepRunning) {
        var++;
}   

}


public static void main(String[] args) {
    Puzzle p=new Puzzle();
    p.setPriority(Thread.MAX_PRIORITY);
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    p.start();
    System.out.println("Background Thread started");
    try { Thread.sleep(1000); } catch(Exception e) { ; } // Dirty hack to give the background thread chance to start :)
    while (var != var) {
        System.out.println(" I am inside loop now! ");
    }

    keepRunning=false; // we escaped from the loop, switch off the background thread.
}

}