java实现C语言中的fflush函数原理

C语言中有一个fflush函数,作用是清除缓冲区的垃圾值

然而作为更为安全的java,包中却没有类似的方法。

现在,我们可以自己模拟C语言的fflush函数,写出java的一个方法

package test1;

import java.util.Scanner;

public class test07{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		int n = 0;
		while(!input.hasNextInt()){
			input.next();
			System.out.println("你错啦,重来");
		}
		n = input.nextInt();
		System.out.println(n);
	}
}

猜你喜欢

转载自blog.csdn.net/mathew_leung/article/details/79830250