当前位置:网站首页>After the input error of next numerical data type () occurs, it can still be input normally next time
After the input error of next numerical data type () occurs, it can still be input normally next time
2022-07-23 20:17:00 【SSS4362】
next Numerical data type () After an input error occurs , You can still input normally next time
1 The scene
1.1 Content
next Numerical data () Method means nextInt()、nextDouble()、nextByte() Equal value receiving method
use try…catch Wrap the digital receiving method , When the input is a string , The program will not be interrupted , The user will be prompted to enter again , Call again nextInt() When the method is used , You will find that the user can't input directly , Directly into the dead circle , But there are always input prompt statements
1.2 Sample code
package Work;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
boolean flag=true;
// When no exception occurs , Set it to false, Just exit the loop
int n=0;
while(flag){
System.out.println(" Please enter a number n:");
try {
n=s.nextInt();
flag=false;
} catch (InputMismatchException e) {
System.out.println(" Incorrect input , Please re-enter !!!!");
} catch (Exception e){
}
}
}
}
1.3 The sample code runs

2 Why is this problem
use nextInt() Method to receive the data input by the user , Meeting First check whether there is data in the space , If it does not exist, the user will need to enter a data , After entering the data and pressing enter, the next statement will be executed , At this time, the user's data will be put into a space
When the user enters the type and nextInt() Approach is to The match is successful Of , It will take out the data stored in the space , Then put the value in the space Empty
When the user enters the type and nextInt() Methods The match didn't work when , At this time, the value in the space Will not be taken out , Next time you call nextInt() I still got this value when I was , Because the value can be obtained in the space, there is no need for users to enter the value , Also used try…catch Exception handling , This program will not be interrupted , So the program has been executed
3 How to solve this problem
3.1 emphasis
You have to put The error information in the space is absorbed and released ,
3.2 Solution
stay catch The code block says next() Method is used to receive error messages in space , So that the data in the space can be released
3.3 Example code for solving
package Work;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
boolean flag=true;
// When no exception occurs , Set it to false, Just exit the loop
int n=0;
while(flag){
System.out.println(" Please enter a number n:");
try {
n=s.nextInt();
flag=false;
} catch (InputMismatchException e) {
s.next();
// Used to absorb the error information entered before
System.out.println(" Incorrect input , Please re-enter !!!!");
} catch (Exception e){
}
}
System.out.println(" The program ended normally !!!");
}
}
3.4 Screenshot of solution example

边栏推荐
- ant接口用什么天线_电视后面有个接口写的是 标准ANT 75 欧输入,请问是什么意思,是天线吗?可不可以接闭路线啊?…「建议收藏」
- 【Jailhouse 文章】A Novel Software Architecture for Mixed Criticality Systems(2020)
- MongoDB-查询语句中逻辑运算符not、and、or、nor用法介绍
- 百度地图数据可视化
- EXCEL的密码相关
- New product listing | A-share floor derivatives market point
- 数组——209.长度最小的子数组
- 能量原理与变分法笔记17:广义变分原理(识别因子方法)
- Energy principle and variational method note 15: solution of differential element method
- [深入研究4G/5G/6G专题-40]: URLLC-11-《3GPP URLLC相关协议、规范、技术原理深度解读》-5-5G Qos原理与架构: 切片、PDU会话、QosFlow、5QI、DRB
猜你喜欢

Leetcode 152. product maximum subarray (brute force cracking can actually pass!)

Principe de l'énergie et méthode variationnelle note 19: principe de l'énergie résiduelle minimale + principe du travail possible

Failure after reinstalling the system (error: Reboot and select proper boot device or insert boot media in selected boot device)

多子系统多业务模块的复杂数据处理——基于指令集物联网操作系统的项目开发实践

Win11没有Word文档怎么办?Win11没有Word文档解决教程

AtCoder B - Pizza

Prepare for pressure test with JMeter and visualvw

编译器LLVM-MLIR-Intrinics-llvm backend-instruction

【ASP.NET Core】选项模式的相关接口

【C语言】通讯录(静态版本)
随机推荐
How to add to-do items for win11 widgets? Win11 method of adding to-do widget
Prepare for pressure test with JMeter and visualvw
梅科尔工作室-华为14天鸿蒙设备开发实战笔记四
Energy principle and variational method note 17: generalized variational principle (identification factor method)
Relevant interfaces of [asp.net core] option mode
QT 设置缓存和编译输出路径
梅科尔工作室-华为14天鸿蒙设备开发实战笔记五
2022/7/22 训练日志
Parity rearrangement of Bm14 linked list
Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 5
能量原理与变分法笔记14:总结+问题的求解
Leetcode 152. product maximum subarray (brute force cracking can actually pass!)
Complex data processing of multi subsystem and multi business modules -- project development practice based on instruction set Internet of things operating system
ODrive应用 #6 编码器
线性代数行列式计算方法之降阶法
【Unity项目实践】关卡解锁
Chinese [easy to understand] cannot be set when installing SVN localization package
Adobe Acrobat两个强大的插件
【Jailhouse 文章】A Novel Software Architecture for Mixed Criticality Systems(2020)
Viewing the "Empathy" energy of iqoo 10 pro from 200W super flash charging