java如何判断输入的是数字还是字母
代码如下importjava.util.Scanner;publicclassnomb{publicstaticvoidmain(String[]args){System...
代码如下
import java.util.Scanner;
public class nomb {
public static void main(String[] args) {
System.out.print("please input the number:");
Scanner in=new Scanner(System.in);
int a=in.nextInt();
if (a>0)
System.out.print(a+"是一个正数。");
else
{if (a==0)
System.out.print("您输入的数字是零。");
else
System.out.print(a+"是一个负数。");
}
}
}
如果是如的是字母总是提示
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at nomb.main(nomb.java:11)
想知道怎样才能输入字母输出“input error” 展开
import java.util.Scanner;
public class nomb {
public static void main(String[] args) {
System.out.print("please input the number:");
Scanner in=new Scanner(System.in);
int a=in.nextInt();
if (a>0)
System.out.print(a+"是一个正数。");
else
{if (a==0)
System.out.print("您输入的数字是零。");
else
System.out.print(a+"是一个负数。");
}
}
}
如果是如的是字母总是提示
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at nomb.main(nomb.java:11)
想知道怎样才能输入字母输出“input error” 展开
6个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询