0

私は本当に単純なJavaコードを持っています:

public class Main {

    public static void main(String[] args) {

        int c = 0;

        while (c < 10);
        {
            System.out.println(c);
            c++;
        }

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

}

問題は、while ループが開始されず、プログラムが 7 行目 (while ループの先頭) で待機し続けることです。

私はWindowsでこのコードを完全に使用Oracle Java JDK 1.6u33しています。Xubuntu 12.04.このコードをUbuntuで正しく実行するにはどうすればよいですか?

私がしたこと(およびスレッドダンプ):

gopher@orion:~/Projects/test/src$ javac Main.java
gopher@orion:~/Projects/test/src$ jar cvfm test.jar manifest.txt Main.class
added manifest
adding: Main.class(in = 489) (out= 338)(deflated 30%)
gopher@orion:~/Projects/test/src$ java -jar test.jar
^\2012-08-04 10:26:23
Full thread dump Java HotSpot(TM) 64-Bit Server VM (20.8-b03 mixed mode):

"Low Memory Detector" daemon prio=10 tid=0x00007fbf080b4800 nid=0xa26 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" daemon prio=10 tid=0x00007fbf080b2800 nid=0xa25 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" daemon prio=10 tid=0x00007fbf080af800 nid=0xa24 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=10 tid=0x00007fbf080ad800 nid=0xa23 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=10 tid=0x00007fbf08091000 nid=0xa22 in Object.wait() [0x00007fbf0d1f3000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000000f5b61300> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:118)
    - locked <0x00000000f5b61300> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:134)
    at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:159)

"Reference Handler" daemon prio=10 tid=0x00007fbf0808f000 nid=0xa21 in Object.wait() [0x00007fbf0d2f4000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000000f5b611d8> (a java.lang.ref.Reference$Lock)
    at java.lang.Object.wait(Object.java:485)
    at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:116)
    - locked <0x00000000f5b611d8> (a java.lang.ref.Reference$Lock)

"main" prio=10 tid=0x00007fbf08007800 nid=0xa1d runnable [0x00007fbf0eb4b000]
   java.lang.Thread.State: RUNNABLE
    at Main.main(Main.java:8)

"VM Thread" prio=10 tid=0x00007fbf08088800 nid=0xa20 runnable 

"GC task thread#0 (ParallelGC)" prio=10 tid=0x00007fbf0801a800 nid=0xa1e runnable 

"GC task thread#1 (ParallelGC)" prio=10 tid=0x00007fbf0801c000 nid=0xa1f runnable 

"VM Periodic Task Thread" prio=10 tid=0x00007fbf080bf000 nid=0xa27 waiting on condition 

JNI global references: 887

Heap
 PSYoungGen      total 9216K, used 317K [0x00000000f5b60000, 0x00000000f65a0000, 0x0000000100000000)
  eden space 7936K, 4% used [0x00000000f5b60000,0x00000000f5baf628,0x00000000f6320000)
  from space 1280K, 0% used [0x00000000f6460000,0x00000000f6460000,0x00000000f65a0000)
  to   space 1280K, 0% used [0x00000000f6320000,0x00000000f6320000,0x00000000f6460000)
 PSOldGen        total 21056K, used 0K [0x00000000e1200000, 0x00000000e2690000, 0x00000000f5b60000)
  object space 21056K, 0% used [0x00000000e1200000,0x00000000e1200000,0x00000000e2690000)
 PSPermGen       total 21248K, used 2774K [0x00000000dc000000, 0x00000000dd4c0000, 0x00000000e1200000)
  object space 21248K, 13% used [0x00000000dc000000,0x00000000dc2b5bb8,0x00000000dd4c0000)
4

2 に答える 2

4

しばらくして;、それを削除すると機能します

while (c < 10);

上記は 1 行の while() を実行し、c決して増加しないため、無限ループです。に変更します

while (c < 10) // no ; at the end
于 2012-08-04T08:34:14.553 に答える
0

削除する ; while ループから。次のようにします

while (c < 10)
于 2012-08-04T08:35:26.630 に答える