PoeticalJustice

导航

Scanner

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

 

posted on 2017-09-28 21:45  PoeticalJustice  阅读(142)  评论(0编辑  收藏  举报