java.util.Scanner 是 Java5 的新特征,我们可以通过 Scanner 类来获取用户的输入。
下面是创建 Scanner 对象的基本语法:
Scanner s = new Scanner(System.in);
接下来我们演示一个最简单的数据输入,并通过 Scanner 类的 next() 与 nextLine() 方法获取输入的字符串,在读取前我们一般需要 使用 hasNext 与 hasNextLine 判断是否还有输入的数据:
import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 从键盘接收数据 // next方式接收字符串 System.out.println("next方式接收:"); // 判断是否还有输入 if (scan.hasNext()) { String str1 = scan.next(); System.out.println("输入的数据为:" + str1); } scan.close(); } }
执行以上程序输出结果为:
$ javac ScannerDemo.java $ java ScannerDemo next方式接收: runoob com 输入的数据为:runoob
可以看到 com 字符串并未输出,接下来我们看 nextLine。
import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 从键盘接收数据 // nextLine方式接收字符串 System.out.println("nextLine方式接收:"); // 判断是否还有输入 if (scan.hasNextLine()) { String str2 = scan.nextLine(); System.out.println("输入的数据为:" + str2); } scan.close(); } }
执行以上程序输出结果为:
$ javac ScannerDemo.java $ java ScannerDemo nextLine方式接收: runoob com 输入的数据为:runoob com
可以看到 com 字符串输出。
next():
nextLine():
如果要输入 int 或 float 类型的数据,在 Scanner 类中也有支持,但是在输入之前最好先使用 hasNextXxx() 方法进行验证,再使用 nextXxx() 来读取:
import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 从键盘接收数据 int i = 0; float f = 0.0f; System.out.print("输入整数:"); if (scan.hasNextInt()) { // 判断输入的是否是整数 i = scan.nextInt(); // 接收整数 System.out.println("整数数据:" + i); } else { // 输入错误的信息 System.out.println("输入的不是整数!"); } System.out.print("输入小数:"); if (scan.hasNextFloat()) { // 判断输入的是否是小数 f = scan.nextFloat(); // 接收小数 System.out.println("小数数据:" + f); } else { // 输入错误的信息 System.out.println("输入的不是小数!"); } scan.close(); } }
执行以上程序输出结果为:
$ javac ScannerDemo.java $ java ScannerDemo 输入整数:12 整数数据:12 输入小数:1.2 小数数据:1.2
以下实例我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果:
import java.util.Scanner; class ScannerDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); double sum = 0; int m = 0; while (scan.hasNextDouble()) { double x = scan.nextDouble(); m = m + 1; sum = sum + x; } System.out.println(m + "个数的和为" + sum); System.out.println(m + "个数的平均值是" + (sum / m)); scan.close(); } }
执行以上程序输出结果为:
$ javac ScannerDemo.java $ java ScannerDemo 12 23 15 21.4 end 4个数的和为71.4 4个数的平均值是17.85
更多内容可以参考 API 文档:http://www.runoob.com/manual/jdk1.6/。
目录i2c_adapter、i2c_client等结构体分析结构体之间的包含关系i2c_adapter结构体i2c_client结构体I2C系统调用过程整体调用过程调用过程路径1调用过程路径2I2C底层框架底层调用流程例1底层调用流程例2底层调用流程例3用到mux时的流程驱动程序使用流程mux驱动流程ak4954操作流程...
So I have been altering a table in QMF. After 3 alters I believe the table has gone into a pending reorg state so that I cannot alter it additionally. Am I correct in this assumption? If so what impli...
spring的两个核心:BeanFactory(心脏,本身使用)与ApplicationContext(身躯,面向spring框架开发者)一、BeanFactory中的Bean的生命周期 首先看一下BeanFactory的体系结构对其中的几个重要接口进行简单介绍BeanFactory:主要是从容器中获取bean类,通过getBean(String beanName)Lis...
华尔街日报图表How many times have you clicked on a link to a Wall Street Journal article, only to run smack into their for-pay wall? It’s infuriating, you only get to read a few sentences… but you can read t...
应用层通过 adb 命令 即可实现adb shell dumpsys input 获取设备信息找到 触摸屏设备信息(信息内容很多,这里只复制了要用的)小米4Device 6: synaptics_dsx (这个是触摸屏设备名字) Touch Input Mapper: Parameters: GestureMode: spots DeviceType: touchScreen AssociatedDisplay: hasAssociate
URI的格式(URL)
[code="java"]package org.liufei.dccserver;import java.net.InetSocketAddress;import java.util.concurrent.Executors;import org.apache.log4j.Logger;import org.jboss.netty.bootstrap.ServerBoot...
做这题的时候真的是脑子短路了,记录一下。首先已知一个解了,由于有mod n这个东西,所以剩下的解肯定为已知解的x倍mod n,于是在a和b上进行操作就可以了,循环节为n / gcd(n, a),那么a和b的循环节都求出来,他们的lcm就是公共循环节,也就是答案的个数了代码:#include #include #include #include using namespace s
http://blog.csdn.net/ailunlee/article/details/70254835#comments刚开始找了篇博客,博主的配置步骤没有问题,只是版本不兼容,所以为了以后自己忘记所以记录下来,vs3.2.0只能与vs2015匹配,所以按照博主的步骤,vc14换成vc12,然后链接器处opencvworld320改为300即可。
2016年初,微信提出应用号,一时间做传统手机APP的企业都闻之色变,在互联网行业内掀起巨大的波澜。但在2月15日,微信公共平台发布《微信公众平台关于整顿非法分销模式行为的公告》,此公告主要针对微信传销行为。公告中指出,近期,微信方面发现有用户在公众账号利用微信关系链发展下线分销的行为...
关于AOPAOP概述如果说 IoC 是 Spring 的核心,那么面向切面编程就是 Spring 最为重要的功能之一了。面向切面编程(AOP)和面向对象编程(OOP)类似,也是一种编程模式。Spring AOP 是基于 AOP 编程模式的一个框架,它的使用有效减少了系统间的重复代码,达到了模块间的松耦合目的。 AOP 的全称是“Aspect Oriented Programming”,即面向切面编程,它将业务逻辑的各个部分进行隔离,使开发人员在编写业务逻辑时可以专心于核心业务,从而提高了开发效
1、切换到root用户su root2、切换到apt目录cd /etc/apt3、备份文件源文件mv sources.list sources.list_falk4、新建一个sources.lis文件nano sources.lis5.复制如下内容到sources.lis文件# deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xen$deb-src http://archive.