Java Scanner的简单应用
来源:互联网 发布:log4j linux 绝对路径 编辑:程序博客网 时间:2024/06/05 17:20
Scanner 可以用于检测并捕捉键盘输入的各种字符串。
Example 1:
import java.util.Scanner;public class Scanner1 {/** * Shows basic use of the scanner */public static void main(String[] args) {Scanner keyboardInput;keyboardInput = new Scanner(System.in);int first, second;System.out.print("Enter an integer: ");first = keyboardInput.nextInt();System.out.print("Enter another integer: ");second = keyboardInput.nextInt();System.out.println("The first number you entered was " + first);System.out.println("The second number you entered was " + second);int sum = first + second;int product = first * second;System.out.println("Their sum is " + sum);System.out.println("Their product is " + product);keyboardInput.close();}}
输入为:6, 3
输出为:Enter an integer: 6Enter another integer: 3The first number you entered was 6The second number you entered was 3Their sum is 9Their product is 18
Example 2:
import java.util.Scanner;public class Scanner2{public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("What is your favorite book? ");String book = input.nextLine();System.out.print("How much does it weigh (in lbs)? ");double weight = input.nextDouble();System.out.print("How many times have you read it? ");int timesRead = input.nextInt();System.out.println();System.out.println("Since you have read " + book + " " + timesRead + " times,");System.out.println("and " + book + " weighs " + weight + " pounds,");System.out.println("you have read " + timesRead * weight + " pounds worth of " + book+".");input.close();}}
输入为:
the catcher in the rye
3
1
输出为:
What is your favorite book? the catcher in the ryeHow much does it weigh (in lbs)? 3How many times have you read it? 1Since you have read the catcher in the rye 1 times,and the catcher in the rye weighs 3.0 pounds,you have read 3.0 pounds worth of the catcher in the rye.
阅读全文
0 0
- Java Scanner的简单应用
- java 简单的输入BufferedReader和Scanner
- Java, android Scanner 简单的使用
- java.util.Scanner的简单用法
- JAVA之简单scanner
- Scanner类的应用
- Scanner类型的应用
- Scanner的简单例子
- Scanner 简单的例子
- java.util.Scanner应用详解
- java Scanner 应用打印三角形
- java.util.Scanner应用详解
- java.util.Scanner应用详解
- java.util.Scanner应用详解
- java.util.Scanner应用详解
- java.util.Scanner应用详解
- java.util.Scanner应用详解
- java.util.Scanner应用详解
- Android应用程序开发期末大作业-上集
- 想创业,想做个物业问题跟踪系统(业主宝)
- 形式语言与自动机_笔记整理(二)
- wampservice无法安装的处理方法
- 如何快速导入发票清单,开票清单批量导入方法探讨
- Java Scanner的简单应用
- Linux note0x01
- 18.dubbo引用配置缓存、线程栈自动dump、netty4支持
- Android源码在线查看网站
- 全排列
- 【Webpack】3.多入口设置与 html-webpack-pugin 插件详解
- 前端知识体系及一些细碎的叨叨叨
- Android样式开发之你应该学会的layer-list!
- Python中datetime.timedelta的应用