1 import java.util.Scanner;//scanner 是包含在Java.util中的;使用的时候需要调用
2
3 /**
4 * 测试Scanner类的使用、如何接受键盘的输入
5 * @author ZBG34854
6 *
7 */
8 public class TestScanner {
9 public static void test01(){
10 Scanner s = new Scanner(System.in);
11 String str = s.next();//程序运行到next会阻塞,等待键盘的输入;
12 System.out.println("刚才键盘输入:"+str);
13 }
14 public static void test02(){
15 Scanner s = new Scanner(System.in);
16 System.out.println("请输入一个加数:");
17 int a = s.nextInt();
18 System.out.println("请输入被加数:");
19 int b = s.nextInt();
20 int sum = a + b;
21 System.out.println("计算结果:"+sum);
22 }
23 public static void main(String[] args){
24 test02();
25 }
26
27 }
![]()