6-1 jmu-Java-03面向对象基础-Object

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_42623428/article/details/85726145

输入整数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 x=new Computer();
				a[i]=x;
			}
			else if(c=='d') {
				double b=sc.nextDouble();
				a[i]=b;
			}
			else if(c=='i') {
				int t=sc.nextInt();
				a[i]=t;
			}
			else if(c=='s') {
				String s=sc.next();
				a[i]=s;
			}
			else {
				a[i]=null;
			}
		}
		for(int i=n-1;i>=0;i--) {
			if(a[i]!=null)System.out.println(a[i]);
		}

猜你喜欢

转载自blog.csdn.net/qq_42623428/article/details/85726145
6-1