Java 如果输入非整数,如何打印错误?
程序告诉用户输入的整数是零、正、偶数或奇数,还是负、偶数或奇数 我的问题是,如果输入了非整数,我想为错误添加println。看最后一行Java 如果输入非整数,如何打印错误?,java,integer,println,Java,Integer,Println,程序告诉用户输入的整数是零、正、偶数或奇数,还是负、偶数或奇数 我的问题是,如果输入了非整数,我想为错误添加println。看最后一行 import java.util.Scanner; public class IntegerCheck { public static void main(String [] args) { int x; System.out.println("Enter an integer value:"); Scanner in =
import java.util.Scanner;
public class IntegerCheck {
public static void main(String [] args) {
int x;
System.out.println("Enter an integer value:");
Scanner in = new Scanner(System.in);
x = in.nextInt();
//String x = in.nextInt();
if (((x % 2) == 0) && (x< 0))
System.out.println(x + " is a negative, even integer.");
else if (((x % 2) == 0) && (x == 0))
System.out.println(x + " is Zero.");
else if ((x % 2)==0)
System.out.println(x + " is a positive, even integer.");
if (((x % 2) != 0) && (x<0))
System.out.println(x + " is a negative, odd integer.");
else if ((x % 2) != 0)
System.out.println(x + " is a positive, odd integer.");
if (x != 'number')
System.out.println(x + " is not an integer.");
}
}
import java.util.Scanner;
公共类整数检查{
公共静态void main(字符串[]args){
int x;
System.out.println(“输入整数值:”);
扫描仪输入=新扫描仪(系统输入);
x=in.nextInt();
//字符串x=in.nextInt();
如果((x%2)=0)和&(x<0))
println(x+“是一个负的,偶数的整数。”);
else如果((x%2)==0)和&(x==0))
System.out.println(x+“为零”);
如果((x%2)==0),则为else
println(x+“是一个正的,偶数的整数。”);
如果((x%2)!=0)和&(x您可以使用Scanner.nextInt()
抛出的inputmatchException
。将代码包围在try
/catch
块中并捕获inputmatchException
。它看起来像-
try{
x = in.nextInt();
if (((x % 2) == 0) && (x< 0))
System.out.println(x + " is a negative, even integer.");
else if (((x % 2) == 0) && (x == 0))
System.out.println(x + " is Zero.");
else if ((x % 2)==0)
System.out.println(x + " is a positive, even integer.");
if (((x % 2) != 0) && (x<0))
System.out.println(x + " is a negative, odd integer.");
else if ((x % 2) != 0)
System.out.println(x + " is a positive, odd integer.");
}
catch(InputMismatchException e){
System.out.println("You did not enter an integer!");
}
试试看{
x=in.nextInt();
如果((x%2)=0)和&(x<0))
println(x+“是一个负的,偶数的整数。”);
else如果((x%2)==0)和&(x==0))
System.out.println(x+“为零”);
如果((x%2)==0),则为else
println(x+“是一个正的,偶数的整数。”);
如果((x%2)!=0)和&(x您可以使用Scanner.nextInt()
抛出的inputmatchException
。将代码包围在try
/catch
块中并捕获inputmatchException
。它看起来像-
try{
x = in.nextInt();
if (((x % 2) == 0) && (x< 0))
System.out.println(x + " is a negative, even integer.");
else if (((x % 2) == 0) && (x == 0))
System.out.println(x + " is Zero.");
else if ((x % 2)==0)
System.out.println(x + " is a positive, even integer.");
if (((x % 2) != 0) && (x<0))
System.out.println(x + " is a negative, odd integer.");
else if ((x % 2) != 0)
System.out.println(x + " is a positive, odd integer.");
}
catch(InputMismatchException e){
System.out.println("You did not enter an integer!");
}
试试看{
x=in.nextInt();
如果((x%2)=0)和&(x<0))
println(x+“是一个负的,偶数的整数。”);
else如果((x%2)==0)和&(x==0))
System.out.println(x+“为零”);
如果((x%2)==0),则为else
println(x+“是一个正的,偶数的整数。”);
如果((x%2)!=0)和&(x