分析理解:Scanner sc = new Scanner(System.in);
- Scanner:用于接收键盘录入数据。
- 前面的时候:
- A:导包
- B:创建对象
- C:调用方法
- <span style="color: #ff0000;">分析理解:Scanner sc = new Scanner(System.in);
- System类下有一个静态的字段:
- public static final InputStream in; 标准的输入流,对应着键盘录入。
- InputStream is = System.in;
- class Demo {
- public static final int x = 10;
- public static final Student s = new Student();
- }
- int y = Demo.x;
- Student s = Demo.s;
- 构造方法:
- Scanner(InputStream source)
<span style="color: #008000;">*/
<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.Scanner;
<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> ScannerDemo {
<span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
<span style="color: #008000;">//<span style="color: #008000;"> 创建对象
Scanner sc = <span style="color: #0000ff;">new<span style="color: #000000;"> Scanner(System.in);
</span><span style="color: #0000ff;">int</span> x =<span style="color: #000000;"> sc.nextInt();
System.out.println(</span>"x:" +<span style="color: #000000;"> x);
}
}
Scanner类的hasNextInt()和nextInt()方法
<span style="color: #008000;">/*<span style="color: #008000;">
-
基本格式:
-
public<span style="color: #ff0000;"> boolean hasNextXxx():<span style="color: #ff0000;">判断是否是某种类型的元素
-
public <span style="color: #ff0000;">Xxx next<span style="color: #ff0000;">Xxx():获取该元素
-
举例:用int类型的方法举例
-
public boolean hasNextInt()
-
public int nextInt()
-
注意:
-
InputMismatchException:输入的和你想要的不匹配
<span style="color: #008000;">*/
<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> ScannerDemo {
<span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
<span style="color: #008000;">//<span style="color: #008000;"> 创建对象
Scanner sc = <span style="color: #0000ff;">new<span style="color: #000000;"> Scanner(System.in);</span><span style="color: #008000;">//</span><span style="color: #008000;"> <a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>数据</span> <span style="color: #0000ff;">if</span><span style="color: #000000;"> (sc.hasNextInt()) { </span><span style="color: #0000ff;">int</span> x =<span style="color: #000000;"> sc.nextInt(); System.out.println(</span>"x:" +<span style="color: #000000;"> x); } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { System.out.println(</span>"你输入的数据有误"<span style="color: #000000;">); }
}
}
<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.Scanner;
<span style="color: #008000;">/*<span style="color: #008000;">
-
常用的两个方法:
-
public int nextInt():获取一个int类型的值
-
public String nextLine():获取一个String类型的值
-
-
出现问题了:
-
-
主要原因:就是那个换行符号的问题。
-
如何解决呢?
-
<span style="color: #ff0000;"> A:先获取一个数值后,在创建一个新的键盘录入对象获取字符串。
-
B:把所有的数据都先按照字符串获取,然后要什么,你就对应的转换为什么。
<span style="color: #008000;">*/
<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> ScannerDemo {
<span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
<span style="color: #008000;">//<span style="color: #008000;"> 创建对象
Scanner sc = <span style="color: #0000ff;">new<span style="color: #000000;"> Scanner(System.in);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> <a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>两个int类型的值
</span><span style="color: #008000;">//</span><span style="color: #008000;"> int a = sc.nextInt();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> int b = sc.nextInt();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> System.out.println("a:" + a + ",b:" + b);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> System.out.println("-------------------");
</span><span style="color: #008000;">//</span><span style="color: #008000;"> <a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>两个String类型的值
</span><span style="color: #008000;">//</span><span style="color: #008000;"> String s1 = sc.nextLine();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> String s2 = sc.nextLine();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> System.out.println("s1:" + s1 + ",s2:" + s2);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> System.out.println("-------------------");
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 先<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>一个字符串,在<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>一个int值
</span><span style="color: #008000;">//</span><span style="color: #008000;"> String s1 = sc.nextLine();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> int b = sc.nextInt();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> System.out.println("s1:" + s1 + ",b:" + b);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> System.out.println("-------------------");
</span><span style="color: #ff0000;">// 先<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>一个int值,在<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>一个字符串,这里会出问题
</span><span style="color: #008000;">//</span><span style="color: #008000;"> int a = sc.nextInt();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> String s2 = sc.nextLine();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> System.out.println("a:" + a + ",s2:" + s2);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> System.out.println("-------------------");</span>
<span style="color: #0000ff;">int</span> a =<span style="color: #000000;"> sc.nextInt();
Scanner sc2 </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Scanner(System.in);
String s </span>=<span style="color: #000000;"> sc2.nextLine();
System.out.println(</span>"a:" + a + ",s:" +<span style="color: #000000;"> s);
}
}
<span style="color: #008000;">/*<span style="color: #008000;">
-
常用的两个方法:
-
public int nextInt():获取一个int类型的值
-
public String nextLine():获取一个String类型的值
-
出现问题了:
-
主要原因:就是那个换行符号的问题。
-
如何解决呢?
-
<span style="color: #ff0000;"> A:先获取一个数值后,在创建一个新的键盘录入对象获取字符串。
-
B:把所有的数据都先按照字符串获取,然后要什么,你就对应的转换为什么。
<span style="color: #008000;">*/
<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> ScannerDemo {
<span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
<span style="color: #008000;">//<span style="color: #008000;"> 创建对象
Scanner sc = <span style="color: #0000ff;">new<span style="color: #000000;"> Scanner(System.in);</span><span style="color: #008000;">//</span><span style="color: #008000;"> <a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>两个int类型的值 </span><span style="color: #008000;">//</span><span style="color: #008000;"> int a = sc.nextInt(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> int b = sc.nextInt(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> System.out.println("a:" + a + ",b:" + b); </span><span style="color: #008000;">//</span><span style="color: #008000;"> System.out.println("-------------------"); </span><span style="color: #008000;">//</span><span style="color: #008000;"> <a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>两个String类型的值 </span><span style="color: #008000;">//</span><span style="color: #008000;"> String s1 = sc.nextLine(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> String s2 = sc.nextLine(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> System.out.println("s1:" + s1 + ",s2:" + s2); </span><span style="color: #008000;">//</span><span style="color: #008000;"> System.out.println("-------------------"); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 先<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>一个字符串,在<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>一个int值 </span><span style="color: #008000;">//</span><span style="color: #008000;"> String s1 = sc.nextLine(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> int b = sc.nextInt(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> System.out.println("s1:" + s1 + ",b:" + b); </span><span style="color: #008000;">//</span><span style="color: #008000;"> System.out.println("-------------------"); </span><span style="color: #ff0000;">// 先<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>一个int值,在<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>一个字符串,这里会出问题 </span><span style="color: #008000;">//</span><span style="color: #008000;"> int a = sc.nextInt(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> String s2 = sc.nextLine(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> System.out.println("a:" + a + ",s2:" + s2); </span><span style="color: #008000;">//</span><span style="color: #008000;"> System.out.println("-------------------");</span> <span style="color: #0000ff;">int</span> a =<span style="color: #000000;"> sc.nextInt(); Scanner sc2 </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Scanner(System.in); String s </span>=<span style="color: #000000;"> sc2.nextLine(); System.out.println(</span>"a:" + a + ",s:" +<span style="color: #000000;"> s);
}
}