6-1 jmu-Java-03面向对象基础-Object (15 分)
输入整数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方法答案:
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
//这边是你的代码
int n = sc.nextInt();
Object []a = new Object[n];
for(int i = 0;i < n;i++)
{
String b = sc.next();
char s = b.charAt(0);
if(s =='c')
{
//a[i] = null;
a[i] = new Computer();
}
else if(s =='d')
{
double t = sc.nextDouble();
a[i] = t;
}
else if(s == 'i')
{
Integer t = sc.nextInt();
a[i] = t;
}
else if(s == 's')
{
String t = sc.next();
a[i] = t;
}
else
{
a[i] = null;
}
}
for(int i = n-1;i >= 0;i--)
{
if(a[i]==null)
continue;
System.out.println(a[i]);
}
sc.close();
}
}
版权声明:本文为weixin_42110638原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。