当前位置:网站首页>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

边栏推荐
- Viewing the "Empathy" energy of iqoo 10 pro from 200W super flash charging
- AtCoder B - Pizza
- Relevant interfaces of [asp.net core] option mode
- Debian | Can’t locate Debian/Debhelper/Sequence/germinate.pm in @INC
- What if there is no word document in win11? There is no word document solution tutorial in win11
- What if redis breaks down?
- 百度地图数据可视化
- 梅科尔工作室-华为14天鸿蒙设备开发实战笔记五
- ODrive应用 #6 编码器
- [PM2] PM2 common commands
猜你喜欢

MongoDB-查询语句中$exists以及结合$ne、$nin、$nor、$not使用介绍

Leetcode 219. duplicate Element II exists (yes, resolved)

Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4

Energy principle and variational method note 16: solution of virtual displacement principle

solidworkd学习笔记:草图几何关系及编辑

2022DASCTF MAY
![Relevant interfaces of [asp.net core] option mode](/img/2e/847e7541cfc49fd69794089dce2df2.jpg)
Relevant interfaces of [asp.net core] option mode
![[激光器原理与应用-8]: 激光器电路的电磁兼容性EMC设计](/img/98/8b7a4fc3f9ef9b7e16c63a8c225b02.png)
[激光器原理与应用-8]: 激光器电路的电磁兼容性EMC设计

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

数组——59. 螺旋矩阵 II
随机推荐
能量原理与变分法笔记15:微元法的求解
phar反序列化
能量原理与变分法笔记19:最小余能原理+可能功原理
21.mixin混入详解
[ar learning] - II. Environment construction
Leetcode 238. 除自身以外数组的乘积
2022DASCTF MAY
Model loading of assimp Library under QT
Adobe Acrobat two powerful plug-ins
2022山东老博会,山东养老展,中国国际养老服务业展9月举办
TASK03|回归
[激光器原理与应用-8]: 激光器电路的电磁兼容性EMC设计
千呼万唤,5G双卡双通到底有多重要?
Principe de l'énergie et méthode variationnelle note 19: principe de l'énergie résiduelle minimale + principe du travail possible
Edge cloud | 1. overview
Viewing the "Empathy" energy of iqoo 10 pro from 200W super flash charging
[development experience] development project trample pit collection [continuous update]
2022DASCTF MAY
新品上市|A股场内衍生品大盘点
Win11小组件怎么添加待办事项?Win11添加待办事项小组件的方法