package java01;
class IcCard{
long id;
int money;
void showInfo(){
System.out.print("卡片卡号"+id);
System.out.println(",余额"+money+"元");
}
public class java4{
public void main (String [] argv){
IcCard [] manyCard = new IcCard[3];
for (int i=0;i<manyCards.length;i++){
manyCards[i] = new IcCard();
manyCards[i].id = 0x336789AB+i;
manyCards[i].money =100+i*123 ;
}
for (IcCard c : manyCards)
c.showInfo();
}
}运行不成功,显示有错误,请问如何修改
2 回答
隐0123
TA贡献4条经验 获得超0个赞
manyCards未命名,并且作用域错误,你把公开类型的类写在了自创的对象类型里面。若还未明白可参考以下修改后代码
class IcCard{ long id; int money; void showInfo(){ System.out.print("卡片卡号"+id); System.out.println(",余额"+money+"元"); } }//2、在此处规定类IcCard的作用域 public class java4{ public void main (String [] argv){//3、若要使其运行并且不报错需要在此处的void前添加static方能运行(public static void main(String[] arg){}此函数可理解为为程序运行入口) IcCard [] manyCards = new IcCard[3];//1、在此处需修改变量名和以下for循环中的变量的名称一致 for (int i=0;i<manyCards.length;i++){ manyCards[i] = new IcCard(); manyCards[i].id = 0x336789AB+i; manyCards[i].money =100+i*123 ; } for (IcCard c : manyCards) c.showInfo(); } }
添加回答
举报
0/150
提交
取消