Usage of Scanner in Java Programming
Usage of Scanner
JavaSE provides a class named Scanner in the java.util package, specifically designed for input operations. You can use this class to create an object and then utilize its methods to read data from the keyboard.
Specific implementation steps:
- Import the package
import java.util.Scanner;
- Instantiation of Scanner (creating an object of the class)
- Use the Scanner’s
next()
method to obtain input content of a specified type- Scanner does not have a specific
next()
method for obtaining char type data. However, it providesnext()
andnextLine()
for obtaining strings. If you want to obtain character data, you can first get a string and then perform character extraction operations on the string.
- Scanner does not have a specific
1 | Scanner scan = new Scanner(System.in); |
Classic A+B Problem
Input two integers a, b, and output their sum.
1 | import java.io.*; |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 程俞客&称心号!
评论