简体   繁体   中英

Why does in Java using Println or Print affect the order of execution of code?

I am very new to Java and have been trying to figure out this question. Why does taking an input after print and taking it after println differ the order of execution.

    Scanner input = new Scanner(System.in);
            System.out.println("Enter real part of the number:");
            r=input.nextInt();
            System.out.println("Enter imaginary part of the number:");
            i=input.nextInt();

Output

    Enter real part of the number:
     1 
    Enter imaginary part of the number:
     2
    Scanner input = new Scanner(System.in);
            System.out.print("Enter real part of the number:");
            r=input.nextInt();
            System.out.print("Enter imaginary part of the number:");
            i=input.nextInt();

Output

    1
    2
Enter real part of the number:Enter imaginary part of the number:

print - print method is implemented as it prints the text on the console and the cursor remains at the end of the text at the console.

println -On the other hand, println method is implemented as prints the text on the console and the cursor remains at the start of the next line at the console and the next printing takes place from next line.

I found out that the error or problem is specific to apache netbeans. Using a different software to write or run java programs will not face any such issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM