简体   繁体   English

Java Scanner 出现在 print() 之前

[英]Java Scanner is appearing before print()

I am trying to print a string before asking for the input while using Scanner , but it keeps making the input appear before the string.我试图在使用Scanner要求输入之前打印一个字符串,但它一直使输入出现在字符串之前。 I can't find any reason why it's doing this.我找不到它这样做的任何原因。 I have attached the full code below.我在下面附上了完整的代码。

import java.util.Scanner;


public class FizzBuzz {

    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Number: ");
        long number = scanner.nextLong();
        
        if (number % 5 ==0) {
            System.out.print("Fizz");
        }
        else if (number % 3 ==0) {
            System.out.println("Buzz");
        }
        else if ((number % 5 ==0) && (number % 3 ==0)) {
            System.out.println("FizzBuzz");
        }
        else {
            System.out.println(number + " Is neither Fizzable or Buzzable");
        }
    }
    
}

System.out.print may not flush to the screen immediately, but you could flush it explicitly: System.out.print可能不会立即刷新到屏幕上,但您可以明确地刷新它:

System.out.print("Number: ");
System.out.flush(); // here!
long number = scanner.nextLong();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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