| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 688 人关注过本帖
标题:为什么我的程序toString不能输出
只看楼主 加入收藏
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
结帖率:41.67%
收藏
已结贴  问题点数:20 回复次数:2 
为什么我的程序toString不能输出
这个程序要在一个array里面添加数字, 删除数字, 算array里的以用空间,但是我现在就测试了一下加数字,然后我要输出这个数字,为什么我输入完数字以后程序就结束了,是我的toString有问题,还是add这个method有问题? 高手帮我看一下

public class IAS
{
   private int [] a=new int[2];
   
    /*public IAS ()
    {
    }*/
   
    public void add(int num)
    {
        int i=0, size, count=0;
        do
        {
            if(a[i]==0)
            {
               a[i]=num;
               count++;
            }
            else if(a[i]!=0 && i<a.length)
               i++;
            else if(i>=a.length)
            {
               size=a.length*2;
                int a[]=new int[size];
            }
        }while (count==0);   
    }
   
    public void add(int num1, int num2, int num3, int num4)
    {
          int i=0, size, count=0;
        do
        {
            if(a[i]==0 && i+4<a.length)
            {
               a[i]=num1;
                a[i+1]=num2;
                a[i+2]=num3;
                a[i+3]=num4;
               count++;
            }
            else if(a[i]!=0 && i<a.length)
               i++;
            else if(i+4>=a.length)
            {
               size=a.length*2;
                int a[]=new int[size];
            }
        }while (count==0);
    }
   
    public void clear()
    {
       int a[]=new int[2];
        for (int i=0; i<2; i++)
            a[i]=0;
    }
   
    public boolean contains(int num)
    {
        int count=0;
        
        for(int i=0; i<a.length; i++)
        {
            if(a[i]==num)
               count++;
        }
        
        if(count>0)
            return true;
        else
            return false;
    }
   
    /*public IAS copy()
    {
       int size=a.length;
        int[] array=new array[size];
        for(int i=0; i<size; i++)
            array[i]=a[i];
        return array[];        
    }*/
    /*
    public boolean equals(IAS array)
    {
        int count=0;
        
        for(int i=0; i<a.length; i++)
        {
            if(a[i]==array[i])
               count=0;
            else
               count++;
        }
        if(count==0)
           return true;
        else
           return false;
    }*/
   
    public boolean isEmpty()
    {
       int count=0;
        for(int i=0; i<a.length; i++)
        {
            if(a[i]!=0)
               count++;
        }
        if(count==0)
           return true;
        else
           return false;
    }
   
    public void remove(int num)
    {
        for(int i=0; i<a.length; i++)
        {
            if(a[i]==num)
            {
               for(int j=i; j>=0; j--)
                   a[j]=a[j+1];
            }
        }
    }
   
    public int size()
    {
       int count=0;
        for(int i=0; i<a.length; i++)
        {
           if(a[i]!=0)
               count++;
        }
        return count;
    }
   
    public String toString()
    {
       String str= "{ " + a + " }";
        return str;
    }
}

        
import java.util.Scanner;

public class IAStest
{
   public static void main (String[] args)
    {
       int num;
       IAS one=new IAS();
        IAS two=new IAS();
        
        Scanner keyboard=new Scanner(System.in);
        
        System.out.print("Enter a number: ");
        num=keyboard.nextInt();
        
        one.add(num);
        one.toString();                                                  //当我输入完数字后程序就结束了,怎么回事?
    }
}
搜索更多相关主题的帖子: toString 输出 
2010-10-11 02:01
shellingford
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:19
帖 子:228
专家分:1348
注 册:2010-8-9
收藏
得分:10 
Object a=new Object();
System.out.println(a);//a不为null时,自动调用a对象的toString方法

toString并不是像楼主想的那样直接输出到控制台的
2010-10-11 12:25
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:10 
支持ls,事实上它支持很多重载.
lz使用有误,首先请看.
程序代码:
public String toString() 
{
   String str= "{ " + a + " }";
    return str;//这里只是返回字符串而已,并没有输出.
}

所以,需要输出
System.out.println(one.toString());

再看这种情况:(以下情况,toString()方法中有输出,所以可以直接调用)
程序代码:
public String toString() 
{
   String str= "{ " + a + " }";
       System.out.println(str);
    return str;
}

直接调用:
 one.toString();    

 至于为什么输出的是地址,就不要我说了吧。

C#超级群 74862681,欢迎大家的到来!
2010-10-11 23:21
快速回复:为什么我的程序toString不能输出
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017651 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved