import java.util.Scanner;
import System;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
sc.close();
String[] strs = str.split(" ");
for (int i = 0; i < strs.length; i++) {
try {
Integer tmp = Integer.valueOf(strs[i]);
if (tmp >= 0 && tmp <= 255) {
java.lang.System.out.println(strs[i] + " can be converted into a type byte!");
} else {
java.lang.System.out.println(strs[i] + " cannot be converted into a type byte!");
}
} catch (Exception e) {
java.lang.System.out.println(strs[i] + " cannot be converted into a type byte!");
}
try {
Short tmp = Short.parseShort(strs[i]);
java.lang.System.out.println(strs[i] + " can be converted into a type short!");
} catch (Exception e) {
java.lang.System.out.println(strs[i] + " cannot be converted into a type short!");
}
try {
Integer tmp = Integer.valueOf(strs[i]);
java.lang.System.out.println(strs[i] + " can be converted into a type int!");
} catch (Exception e) {
java.lang.System.out.println(strs[i] + " cannot be converted into a type int!");
}
try {
Float tmp = Float.parseFloat(strs[i]);
java.lang.System.out.println(strs[i] + " can be converted into a type float!");
} catch (Exception e) {
java.lang.System.out.println(strs[i] + " cannot be converted into a type float!");
}
try {
Double tmp = Double.valueOf(strs[i]);
java.lang.System.out.println(strs[i] + " can be converted into a type double!");
} catch (Exception e) {
java.lang.System.out.println(strs[i] + " cannot be converted into a type double!");
}
}
}
}
目前没有想到直接用空格进行判断,我这里以换行符进行判断,你可以找一下如何以空格退出来替换 next()
代码
public static void main(String[] args)throws Exception{
InputStream in = System.in;
Scanner sc = new Scanner(System.in);
System.out.println("Example Input:");
String string = sc.next();
List<String> stringList = new ArrayList<>();
while(!("退出".equals(string))){
stringList.add(string);
sc = new Scanner(System.in);
string = sc.next();
}
System.out.println("Simple Output:");
for (String str : stringList){
try {
Integer tmp = Integer.valueOf(str);
if (tmp >= 0 && tmp <= 255) {
System.out.println(str + " can be converted into a type byte!");
} else {
System.out.println(str + " cannot be converted into a type byte!");
}
} catch (Exception e) {
System.out.println(str + " cannot be converted into a type byte!");
}
try {
Short tmp = Short.parseShort(str);
System.out.println(str + " can be converted into a type short!");
} catch (Exception e) {
System.out.println(str + " cannot be converted into a type short!");
}
try {
Integer tmp = Integer.valueOf(str);
System.out.println(str + " can be converted into a type int!");
} catch (Exception e) {
System.out.println(str + " cannot be converted into a type int!");
}
try {
Float tmp = Float.parseFloat(str);
System.out.println(str + " can be converted into a type float!");
} catch (Exception e) {
System.out.println(str + " cannot be converted into a type float!");
}
try {
Double tmp = Double.valueOf(str);
System.out.println(str + " can be converted into a type double!");
} catch (Exception e) {
System.out.println(str + " cannot be converted into a type double!");
}
}
}
运行结果:
Example Input:
123
true
22.93
退出
Simple Output:
123 can be converted into a type byte!
123 can be converted into a type short!
123 can be converted into a type int!
123 can be converted into a type float!
123 can be converted into a type double!
true cannot be converted into a type byte!
true cannot be converted into a type short!
true cannot be converted into a type int!
true cannot be converted into a type float!
true cannot be converted into a type double!
22.93 cannot be converted into a type byte!
22.93 cannot be converted into a type short!
22.93 cannot be converted into a type int!
22.93 can be converted into a type float!
22.93 can be converted into a type double!
Process finished with exit code 0
希望对你有帮助^-^