输入整数n,创建n个对象,放入同一个数组中。
如果输入c
,则new Computer();
//注意:Computer是系统中已有的类,无需自己编写
如果输入d
,则根据随后的输入创建Double
类型对象。
如果输入i
,则根据随后的输入创建Integer
类型对象。
如果输入s
,则根据随后的输入创建String
类型对象。
如果不是以上这些输入,则不创建对象,而是将null
存入数组相应位置。
最后倒序输出数组中的所有对象,如果数组中相应位置的元素为null
则不输出。
裁判测试程序:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//这边是你的代码
sc.close();
}
输入样例:
5
c
d 2.3
other
i 10
s Test
输出样例:
Test
10
2.3
//这行显示Computer对象toString方法
代码如下:
int n = sc.nextInt();
Object A [] = new Object[n];
for (int i = 0; i < n; i ++)
{
String str = sc.next();
char c = str.charAt(0);
if (c == 'c')
{
Computer temp = new Computer();
A[i] = temp;
}
else if (c == 'd')
{
double temp = sc.nextDouble();
A[i] = temp;
}
else if (c == 'i')
{
int temp = sc.nextInt();
A[i] = temp;
}
else if (c == 's')
{
String temp = sc.next();
A[i] = temp;
}
else
{
A[i] = null;
}
}
for (int i = n - 1; i >= 0; i --)
{
if (A[i] != null)
{
System.out.println(A[i]);
}
}