我在一个项目中有三个文件,我似乎无法在我的主类中打印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;
}
}
我运行每一个,所有这些都有一个成功的构建,但没有输出!
答案 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
要检查的事项:
System.out.println("test);
- 看它打印答案 1 :(得分:0)
该代码完美无缺,并且无论如何都没有任何问题。因此,运行代码的方式肯定存在问题。这是一份清单:
最后,最后但并非最不重要:
:-)