Strange behavior in Java, is it just me?
I have the following lines :
for (int i=0;i<3;i++) System.out.print("\nA : "+i);
// System.out.println("");
// for (int i=0;i<3;i++) System.out.println("B : "+i);
The output is :
A : 0
A : 1
A : 2A : 2
Why ? I expected this :
A : 0
A : 1
A : 2
But if I uncomment the 2nd and 3rd lines [ together or one at a time ], it behaved correctly ? What's going on ? Is it my PC problem, or my NB6.7 problem ? I can't believe Java would do this !
Edit :
for (int i开发者_开发问答=0;i<3;i++) System.out.print("A: "+i+"\n")
works correctly as expected.
When I ran it from command line, no problem at all, seems like a NB problem.
Works for me:
public class Test {
public static void main(String [] args) {
for (int i=0;i<3;i++) System.out.print("\nA : "+i);
}
}
Output:
C:\Users\Jon\Test>javac Test.java
C:\Users\Jon\Test>java Test
A : 0
A : 1
A : 2
C:\Users\Jon\Test>
Perhaps Netbeans is just echoing the last line of output when the app terminates?
It works for me as well, but try the following and tell us what it does.
Try putting the newline after i.e. print("A: "+i+"\n");
精彩评论