使用Goods类,创建10个商品,输出所有商品的信息。
(1) 定义数组goodsList ,元素类型为Goods,长度为10。
(2) 创建10个数组元素。
(3) 给10个元素的成员变量赋值。(值可以输入,也可以用常量)
(4) 依次输出10个商品信息。(调用show()方法)
商品参考信息:(可自由定义)
import java.util.*;
public class Goods
{
Goods [] goodsList=new Goods [10];
String name;
double price;
public Goods()
{
}
public Goods(String name,double x)
{
this.name=name;
price=x;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
// String s=sc.next();
//double x=sc.nextDouble();
Goods goodsList[]=new Goods[10];
int i;
for(i=0;i<10;i++)
{
goodsList[i]=new Goods();
goodsList[i].name=sc.next();
goodsList[i].price=sc.nextDouble();
}
System.out.println("Goodsname"+" "+"GoodsPrice");
for(i=0;i<10;i++)
{
System.out.println(goodsList[i].name+" "+goodsList[i].price);
}
}
}