Three methods of obtaining keyboard input values in Java
In the process of program development, it is common to obtain input values from the keyboard, but Java has no ready-made function like scanf() provided by C language and cin() provided by C + + to obtain keyboard input values! Java does not provide such a function, which does not mean that we are helpless in this situation. Please see the following three solutions:
Several methods are listed below:
Method 1: receive a character from the console and print it out
Although this method obtains the input characters from the keyboard, system out. Read () can only obtain one character. At the same time, the type of the obtained variable can only be char. When we enter a number and want to get an integer variable, we have to modify the variable type, which is more troublesome.
Method 2: receive a string from the console and print it out. In this topic, we need to use the BufferedReader class and inputstreamreader class
So we can get the string we entered.
Method 3: I think this method is the simplest and most powerful, which is to use the scanner class
This code has shown that the scanner class, whether for string, integer data or float type variables, only needs to make a small change to realize the function! No doubt he is the most powerful!
summary
The above is all about the three methods of obtaining keyboard input values in Java. I hope it will be helpful to you. Interested friends can continue to refer to this site: detailed explanation of Java escape analysis and code examples, detailed explanation of list in Java collection, function of Java programming interface call and code sharing. If you have any questions, you can leave a message at any time. Xiaobian will reply to you in time. Thank you for your support!