バッファをクリアするJAVAメソッド



Java Method Clear Buffer



/**************************** * File name: [Job] * Author: * Description: * Creation time: 2019.12.31 ****************************/ import java.util.Scanner public class Test { public static void main(String[] args) { Scanner scanner = new Scanner(System.in) int num = 0 while(!scanner.hasNextInt()) {//Determine if the next one is an integer System.out.println('The input is not an integer, please re-enter:')//At this time, user input already exists in the buffer and cannot be received with nextInt. An exception will occur. scanner.next()//Receive the data in the buffer but do not assign a value } num = scanner.nextInt() } }

画像