Println语句不打印

时间:2013-09-22 01:20:11

标签: java arrays string logging output

我在一个项目中有三个文件,我似乎无法在我的主类中打印println语句!帮助

第一档:

package chapter2;

public class UseStringLog
{
    public static void main(String[] args)
    { 
    StringLogInterface log;
    log = new ArrayStringLog("Example Use");
    log.insert("Elvis");
    log.insert("King Louis XII");
    log.insert("Captain Kirk");
    System.out.println(log);
    System.out.println("The size of the log is " + log.size());
    System.out.println("Elvis is in the log: " + log.contains("Elvis"));
    System.out.println("Santa is in the log: " + log.contains("Santa"));
    }
}

第二档:

package chapter2;

public interface StringLogInterface
{
  void insert(String element);
  boolean isFull();
  int size();
  boolean contains(String element);
  void clear();
  String getName();
  String toString();
}

第三档:

package chapter2;

public class ArrayStringLog implements StringLogInterface
{
  protected String name;              
  protected String[] log;             
  protected int lastIndex = -1;       

  public ArrayStringLog(String name, int maxSize)
  {
    log = new String[maxSize];
    this.name = name;
  }

  public ArrayStringLog(String name) 
  {
    log = new String[100];
    this.name = name;
  }

  public void insert(String element)
  {      
    lastIndex++;
    log[lastIndex] = element;
  }

  public boolean isFull()
  {              
    if (lastIndex == (log.length - 1)) 
      return true;
    else
      return false;
  }

  public int size()
  {
    return (lastIndex + 1);
  }

  public boolean contains(String element)
  {                 
    int location = 0;
    while (location <= lastIndex) 
    {
      if (element.equalsIgnoreCase(log[location]))  // if they match
        return true;
      else
        location++;
    }
   return false;
  }

  public void clear()
  {                  
    for (int i = 0; i <= lastIndex; i++)
      log[i] = null;
    lastIndex = -1;
  }

  public String getName()
  {
    return name;
  }

  public String toString()
  {
    String logString = "Log: " + name + "\n\n";

    for (int i = 0; i <= lastIndex; i++)
      logString = logString + (i+1) + ". " + log[i] + "\n";

    return logString;
  }
}

我运行每一个,所有这些都有一个成功的构建,但没有输出!

2 个答案:

答案 0 :(得分:0)

你的代码工作得很好!

<强>输出:

Log: Example Use

1. Elvis
2. King Louis XII
3. Captain Kirk

The size of the log is 3
Elvis is in the log: true
Santa is in the log: false

要检查的事项:

  • 编译错误(您没有在包中包含某个类,忘记了“导入”等)
  • 尝试在main()的第一行调用System.out.println("test); - 看它打印
  • 使用调试器运行代码

答案 1 :(得分:0)

该代码完美无缺,并且无论如何都没有任何问题。因此,运行代码的方式肯定存在问题。这是一份清单:

  • 确保您运行的是正确的类文件。
  • 如果您使用的是IDE,请确保知道如何使用它。
  • 如果你完成了本书的第1章,你可能已经设法在那里运行程序。尝试相同的方法。

最后,最后但并非最不重要:

  • 检查智商。如果小于150,建议您远离java编码。:-)