scan
=
new
scanner(system.in)
//
第一种方式,直接用nextint方法,这样就限制了用户在控制台只能输入数字了
int
i
=
scan.nextint()
//
第二种,给用户输入字符串的操作
string
s
=
scan.next()
while
(true)
{
try
{
i
=
integer.parseint(s)
break//
假如强转成功,则终止循环
}
catch
(exception
e)
{
system.out.println("输入的"
+
s
+
"不是数字,重新输入")
s
=
scan.next()//
强转失败,继续输入
}
}
//
第三种,直接比较,不过只限于0-9
system.out.println("请输入一个字符!")
string
str
=
scan.next()
if
(str.matches("[0-9]"))
{
system.out.println("是数字")
}
else
{
system.out.println("不是数字")
}
Object[] o = new Object[2]o[0] = 1
o[1] = "aaa"
for (int i = 0 i < o.length i++) {
if (o[i] instanceof Integer) {
int num = (int) o[i]
} else if (o[i] instanceof String) {
String str = (String) o[i]
}
}
java里没有typerof ,要用instanceof
基础类型不行,要用对象
Integer i = 0
if (i instanceof Integer) {
System.out.println("haha")
}
String str = "abc"
if (str instanceof Object) {
System.out.println("haha")
}