public class Customer {
int custNo=0;
int point=0;
}public class CustManager {
Customer[] customers = new Customer[100];
public void add(Customer cust){
for(int i=0;i<customers.length;i++){
if(customers[i]==null){
customers[i]=cust;
break;
}
}
}
public void show(){
System.out.println("编号\t积分");
for(int i=0;i<customers.length;i++){
if(customers[i]==null)
break;
System.out.println(customers[i].custNo+"\t"+customers[i].point);
}
}
}import java.util.Scanner;public class TestCustomer { public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
Customer cust=new Customer();
CustManager custm=new CustManager();
for(int j=0;j<100;j++){
System.out.print("请输入会员编号:");
cust.custNo=sc.nextInt();
System.out.print("请输入会员积分:");
cust.point=sc.nextInt();
custm.add(cust);
if(cust.point==0)
break;
}
custm.show();
}}
当积分输入为0时,退出输入,显示输入的卡号和积分、、、
输出错误结果:
int custNo=0;
int point=0;
}public class CustManager {
Customer[] customers = new Customer[100];
public void add(Customer cust){
for(int i=0;i<customers.length;i++){
if(customers[i]==null){
customers[i]=cust;
break;
}
}
}
public void show(){
System.out.println("编号\t积分");
for(int i=0;i<customers.length;i++){
if(customers[i]==null)
break;
System.out.println(customers[i].custNo+"\t"+customers[i].point);
}
}
}import java.util.Scanner;public class TestCustomer { public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
Customer cust=new Customer();
CustManager custm=new CustManager();
for(int j=0;j<100;j++){
System.out.print("请输入会员编号:");
cust.custNo=sc.nextInt();
System.out.print("请输入会员积分:");
cust.point=sc.nextInt();
custm.add(cust);
if(cust.point==0)
break;
}
custm.show();
}}
当积分输入为0时,退出输入,显示输入的卡号和积分、、、
输出错误结果:
解决方案 »
- 在控制台下编译执行package过后的java文件……怎么找到路径?
- 问一个继承的问题
- 一道程序设计题....用java如何实现
- 关于用java6.0的方法编译java程序的问题
- 请问下怎样在JTextField中添加数据库查询结果ResultSet
- 为什么页面没有显示,急
- 请大家帮忙解决一个问题
- 请教各位高手关于java连接动态库问题(在线等待!)
- 如何建立一个非模式的Dialog窗口
- 请问tomcat的classpath在那个文件中设置?
- 求救,报错:java.lang.OutOfMemoryError:Java heap space
- 小菜鸟问一个关于 PriorityQueue 的困惑 求大神解答
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
Customer cust=new Customer();
CustManager custm=new CustManager();for(int j=0;j<100;j++){
System.out.print("请输入会员编号:");
cust.custNo=sc.nextInt();
System.out.print("请输入会员积分:");
cust.point=sc.nextInt();
custm.add(cust);
if(cust.point==0)
break;
这段代码中只new出了一个Customer 对象cust,在每次调用add()方法时,
都是把cust的内存地址传递给cumstom[i],换句话说就是数组customers的每个元素指向同一个内存地址,
每个元素的custno和point都会一样。无论怎么添加,数组的元素都会是最后添加的那个。
Customer cust=new Customer();
System.out.print("请输入会员编号:");
cust.custNo=j;
System.out.print("请输入会员积分:");
cust.point=j+1;
custm.add(cust);
custm.show();
if(cust.point==0)
break;
}