当前位置:网站首页>User interaction scanner usage Advanced Edition example
User interaction scanner usage Advanced Edition example
2022-06-25 23:23:00 【Seasonal white September】
List of articles
User interaction scanner
java.util.Scanner yes java5 New features , Can pass Scanner Class to get user input
Basic grammar :
Scanner s=new Scanner(System.in);
adopt Scanner Class next() And nextLine() Method to get the input string , We usually need to Use hasNext() And hasNextLine() Determine whether there is any input data .
next() and hasNext() Method
You must read valid characters before you can end the input
Whitespace encountered before entering valid characters for ,next() Method will automatically remove it
** Such as Input hello world The output is hello **
Only after entering a valid character can the blank space entered after it be used as a separator or Terminator
next() Cannot get string with space
package com.zhang.scanner;
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
// Create a scanner object , For receiving keyboard data
Scanner scanner=new Scanner(System.in);
System.out.println(" Use next Mode reception :");
// Determine whether the user has input string
if (scanner.hasNext()){
// Use next Mode to receive
String str=scanner.next();// The program will wait for the user to input
System.out.println(" The output is :"+str);
}
// All belong to IO If the class of the stream is not closed all the time Will always take up resources , To form a good habit, turn it off after use
scanner.close();
}
}
Use next Mode reception :
hello world
The output is :hello
- nextLine() and hasNextLine()
- With Enter End of character in other words nextLine() Method returns all the characters before enter
- Can get blank
package com.zhang.scanner;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
// Receive data from keyboard
Scanner scanner=new Scanner(System.in);
System.out.println(" Use nextLine Mode reception :");
// Judge whether there is any input
if (scanner.hasNextLine()){
String str=scanner.nextLine();
System.out.println(" The output is :"+str);
}
scanner.close();
}
}
Use nextLine Mode reception :
hello world
The output is :hello world
package com.zhang.scanner;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
// Receive data from keyboard
Scanner scanner = new Scanner(System.in);
System.out.println(" Please input data :");
String str=scanner.nextLine();
System.out.println(" The output is :" + str);
scanner.close();
}
}
Please input data :
I love you! China
The output is : I love you! China
expand
package com.zhang.scanner;
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
// Receive data from keyboard
int i;
float f=0.0f;
System.out.println(" please enter an integer :");
// If ... that
if (scanner.hasNextInt()){
i=scanner.nextInt();
System.out.println(" Integer data :"+i);
}else {
System.out.println(" The input is not integer data !");
}
System.out.println(" Please enter the decimal :");
if (scanner.hasNextFloat()){
f=scanner.nextFloat();
System.out.println(" Fractional data :"+f);
}else {
System.out.println(" Input is not decimal data !");
}
scanner.close();
}
}
please enter an integer :
10.1
The input is not integer data !
Please enter the decimal :
Fractional data :10.1
We can enter multiple numbers , And find the sum and the mean , Press enter to confirm each number entered , End the input and output the execution result by inputting non number
package com.zhang.scanner;
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
// We can enter multiple numbers , And find the sum and the mean , Press enter to confirm each number entered , End the input and output the execution result by inputting non number
Scanner scanner=new Scanner(System.in);
// and
double sum=0;
// Calculate how many numbers are entered
int m=0;
// Loop to see if there is any input , And sum up each time in it
while (scanner.hasNextDouble()){
double x=scanner.nextDouble();
m=m+1;//m++
sum=sum+x;
System.out.println(" You entered the number one "+m+" Data , The current result sum="+sum);
}
System.out.println(m+" The sum of the numbers is "+sum);
System.out.println(m+" The average number is "+(sum/m));
scanner.close();
}
}
10
You entered the number one 1 Data , The current result sum=10.0
20
You entered the number one 2 Data , The current result sum=30.0
30
You entered the number one 3 Data , The current result sum=60.0
guyguy
3 The sum of the numbers is 60.0
3 The average number is 20.0
边栏推荐
- What do l and R of earphone mean?
- Trillions of hot money smashed into the space economy. Is it really a good business?
- Unity技术手册 - 粒子发射和生命周期内速度子模块
- adb常用命令
- 为什么OpenCV计算的帧率是错误的?
- ES6 -- formal parameter setting initial value, extension operator, iterator, and generating function
- ADB common commands
- .sql数据库导入错误:/*!40101 SET @[email protected]@COLLATION_CONNECTION */
- ORACLE - 数据查询
- PDM fur
猜你喜欢

【ModuleBuilder】GP服务实现SDE中两个图层相交选取

LM small programmable controller software (based on CoDeSys) note XVII: PTO pulse function block
Why is BeanUtils not recommended?

Circuit module analysis exercise 6 (switch)

Network security project questions of the first Henan vocational skills competition in 2022

STM32开发板+机智云AIoT+家庭监测控制系统

电路模块分析练习6(开关)

UE4\UE5 蓝图节点Delay与Retriggerable Delay的使用与区别

Multi modal data can also be Mae? Berkeley & Google proposed m3ae to conduct Mae on image and text data! The optimal masking rate can reach 75%, significantly higher than 15% of Bert

Live800 online customer service system: do business across time and space, starting from each interaction
随机推荐
首个大众可用PyTorch版AlphaFold2复现,哥大开源OpenFold,star量破千
C language and the creation and use of database
ES6 -- formal parameter setting initial value, extension operator, iterator, and generating function
剑指 Offer 46. 把数字翻译成字符串(DP)
Set up your own website (15)
MySQL数据库索引
2. What is the geometric meaning of a vector multiplying its transpose?
The sum of logarithms in group 52--e of Niuke Xiaobai monthly race (two points)
Use and difference between ue4\ue5 blueprint node delay and retroggable delay
zabbix_server配置文件详解
NLP text summary: use the pre training model to perform text summary tasks [transformers:pipeline, T5, Bart, Pegasus]
Utilisation de la classe Ping d'Unity
小程序绘制一个简单的饼图
腾讯《和平精英》新版本将至:新增账号安全保护系统,游戏内违规行为检测升级
Flex & Bison 開始
Civil Aviation Administration: by 2025, China will initially build a safe, intelligent, efficient and green aviation logistics system
c语言与数据库的创建使用
What are the channels for Internet advertising to gain customers?
Unity technical manual - particle foundation main module attributes - upper
提问的智慧?如何提问?