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

边栏推荐
- Leetcode 238. product of arrays other than itself
- 百度地图数据可视化
- 【无标题】
- Applet avatar group style
- Lecture 9 of project practice -- operation import and export tool
- 非局部均值滤波(NON-LOCAL-mean)/注意力机制
- 能量原理与变分法笔记15:微元法的求解
- Relevant interfaces of [asp.net core] option mode
- Prepare for pressure test with JMeter and visualvw
- BM14 链表的奇偶重排
猜你喜欢

Energy principle and variational method note 12: minimum potential energy principle

Leetcode 238. product of arrays other than itself

如何给电脑系统重置系统?方法其实很简单

next数值型数据类型()出现输入错误后,下次依然能正常输入

I deliberately leave a loophole in the code. Is it illegal?

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

如何合理地估算线程池大小

梅科爾工作室-小熊派開發筆記2

梅科尔工作室-小熊派开发笔记2

MongoDB-查询语句中逻辑运算符not、and、or、nor用法介绍
随机推荐
2022山东养老展,中国国际养老服务业展览会,济南老龄产业展
Top ten shrinking industries in China in the first half of 2022
Atelier macoll - notes de développement de la secte de l'ours 2
梅科尔工作室-华为14天鸿蒙设备开发实战笔记四
Energy principle and variational method note 18: virtual force principle
Adobe Acrobat两个强大的插件
Leetcode 152. 乘积最大子数组(暴力破解居然可以通过!)
17.生命周期
Win11没有Word文档怎么办?Win11没有Word文档解决教程
Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 6
absl教程(四):Strings Library
梅科尔工作室-小熊派开发笔记3
When using polymorphism, two ideas to judge whether it can be transformed downward
21. Mix in details
梅科尔工作室-华为14天鸿蒙设备开发实战笔记六
Leetcode 238. product of arrays other than itself
Leetcode 219. duplicate Element II exists (yes, resolved)
Mecol Studio - Little Bear Development Notes 3
【C语言】通讯录(静态版本)
Task03 | return