键入字符/字符串而不是整数时输出错误 package项目2; 导入java.util.*; 公共类项目2{ 公共静态void main(字符串[]args){ 字符串名; 智力年龄; 扫描仪输入=新扫描仪(System.in); 系统输出打印(“输入您的年龄:”; age=input.nextInt(); 如果(年龄>=16岁){ System.out.println(“你的好东西”);} 如果((年龄>=1)&(年龄
你有没有问过某人的年龄,他告诉你:键入字符/字符串而不是整数时输出错误 package项目2; 导入java.util.*; 公共类项目2{ 公共静态void main(字符串[]args){ 字符串名; 智力年龄; 扫描仪输入=新扫描仪(System.in); 系统输出打印(“输入您的年龄:”; age=input.nextInt(); 如果(年龄>=16岁){ System.out.println(“你的好东西”);} 如果((年龄>=1)&(年龄,java,input,output,java.util.scanner,Java,Input,Output,Java.util.scanner,你有没有问过某人的年龄,他告诉你:q?这不是年龄,也不是int 您得到异常是因为: 如果下一个令牌与 整数正则表达式,或超出范围 如果我输入任何字母,都会显示“无效输入”。有人能帮我吗 捕获异常并打印消息,如下所示- package project2; import java.util.*; public class Project2 { public static void main(String[] args) { String Name; int age;
q
?这不是年龄,也不是int
您得到异常是因为:
如果下一个令牌与
整数正则表达式,或超出范围
如果我输入任何字母,都会显示“无效输入”。有人能帮我吗
捕获异常并打印消息,如下所示-
package project2;
import java.util.*;
public class Project2 {
public static void main(String[] args) {
String Name;
int age;
Scanner input = new Scanner(System.in);
System.out.print("Enter your age : ");
age = input.nextInt();
if (age>=16){
System.out.println("Your good to go");}
else if ((age>=1)&&(age<=15)){
System.out.println("Your not allowed to Procced");}
else{
System.out.println("Out of range");}
}
}
在使用input.nextInt()
读取int之前,请使用input.hasNextInt()
进行检查,它的读数刚好足以判断它是否为int(但留给nextInt读取;它不会弄乱任何东西),然后返回该值
try{
System.out.print("Enter your age : ");
age = input.nextInt();
}catch(RuntimeException ex){
System.out.println("Invalid input");
}
试试这个:
if(!input.hasNextInt())
System.out.println("Not an int");
else
{
int age = input.nextInt();
// other code here
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int年龄=0;
布尔值isContinue=true;
做{
试一试{
系统输出打印(“输入您的年龄:”;
字符串age1=input.next();
年龄=整数.parseInt(年龄1);
isContinue=false;
如果(年龄>=16岁){
System.out.println(“你的好去处”);
}else if((age>=1)和&(age将输入作为字符串,并检查它是否为数字
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int age = 0;
boolean isContinue = true;
do {
try {
System.out.print("Enter your age : ");
String age1 = input.next();
age = Integer.parseInt(age1);
isContinue = false;
if (age >= 16) {
System.out.println("Your good to go");
} else if ((age >= 1) && (age <= 15)) {
System.out.println("Your not allowed to Procced");
} else {
System.out.println("Out of range");
}
} catch (NumberFormatException e) {
System.out.println("Not an valid age");
}
} while (isContinue);
System.out.println("other code");
}
使用异常处理
Scanner input = new Scanner(System.in);
System.out.print("Enter your age : ");
String rawAge = input.next();
try {
int age = Integer.parseInt(rawAge);
System.out.println("Age: " + age);
// Do something with age
} catch (NumberFormatException e) {
System.out.println("Invalid input for age");
}
import java.util.*;
公开课
{
公共静态void main(字符串[]args)
{
弦年龄;
扫描仪输入=新扫描仪(System.in);
系统输出打印(“输入您的年龄:”;
Age=input.nextLine();
尝试
{
int age=Integer.parseInt(age);
如果(年龄>=16岁)
{
System.out.println(“你的好去处”);
}
否则如果((age>=1)和(&)(age)您预期会发生什么?q
不是int
,nextInt()
需要一个整数,对吗?如果键入一个字母,它会显示“无效输入”嘿Subhrajyoti Majumder!谢谢!)
import java.util.*;
public class crack
{
public static void main(String[] args)
{
String Age;
Scanner input = new Scanner(System.in);
System.out.print("Enter your age : ");
Age = input.nextLine();
try
{
int age = Integer.parseInt(Age);
if (age>=16)
{
System.out.println("Your good to go");
}
else if ((age>=1)&&(age<=15))
{
System.out.println("Your not allowed to Procced");
}
}
catch(NumberFormatException nFE)
{
System.out.println("out of range");}
}
}
}