public class input { public static void main(String[] args) { String aa = args[0]; if (intflg(aa)) { System.out.println("Integer type!"); } else{ System.out.println("not Integer type!"); } } static boolean intflg(String str) { int index; String intArray = "0123456789"; if(str==null || str.equals("")){ return false; } for (int i = 0; i < str.length(); i++) { index = intArray.indexOf(str.charAt(i)); if (index == -1) { return false; } } return true; } }
public class input { public static void main(String[] args) { String aa = args[0]; if (isInteger(aa)) { System.out.println("Integer type!"); } else{ System.out.println("not Integer type!"); } } static boolean isInteger(String strExp) { try { Long x = new Long(strExp); } catch (Exception e) { return false; } return true; } }这样好像更简单
我用的 int num = 0; Scanner sc = new Scanner(System.in);try { num = sc.nextInt(); } catch(java.util.InputMismatchException e) { System.out.println("error"); }好像也能做到
String aa = args[0];
if (intflg(aa)) {
System.out.println("Integer type!");
}
else{
System.out.println("not Integer type!");
}
} static boolean intflg(String str) {
int index;
String intArray = "0123456789";
if(str==null || str.equals("")){
return false;
}
for (int i = 0; i < str.length(); i++) {
index = intArray.indexOf(str.charAt(i));
if (index == -1) {
return false;
}
}
return true;
}
}
String aa = args[0];
if (isInteger(aa)) {
System.out.println("Integer type!");
}
else{
System.out.println("not Integer type!");
}
} static boolean isInteger(String strExp) {
try {
Long x = new Long(strExp);
}
catch (Exception e) {
return false;
}
return true;
}
}这样好像更简单
int num = 0;
Scanner sc = new Scanner(System.in);try
{
num = sc.nextInt();
}
catch(java.util.InputMismatchException e)
{
System.out.println("error");
}好像也能做到
但我傾向于el因為代碼少