我正在研究一个程序,它会将随机数添加到数组中,直到它将一个重复数字放到数组中,然后它应该打印生成重复数字之前生成了多少数字。为什么我收到此运行时异常?
当我运行该程序时,我收到此错误。
Exception in thread "main" java.lang.ExceptionInInitializerError
at arrayintlog.TestLuck.main(TestLuck.java:25)
Caused by: java.lang.RuntimeException: Uncompilable source code - arrayintlog.ArrayIntLog is not abstract and does not override abstract method contains(java.lang.String) in arrayintlog.IntLogInterface
at arrayintlog.ArrayIntLog.<clinit>(ArrayIntLog.java:6)
... 1 more
Java Result: 1
我不知道这个例外的含义。我以前只做过一个接口,而且几乎完全一样,但是有一个字符串数组,我没有问题。
有了这一个NetBeans不断告诉我将抽象添加到我的ArrayIntLog类,但如果我这样做,我的ArrayIntLog构造函数不能在我的主类中工作?
我在做什么错误/缺少这个?
这里是我的主类
package arrayintlog;
import java.util.Random;
import java.util.Scanner;
public class TestLuck
{
public static void main(String[] args)
{
int cycles = 0;
String name;
int min = 1;
int max = 10000;
int duplicate;
Random rand = new Random();
int random = rand.nextInt(max - min + 1) + min;
Scanner in = new Scanner(System.in);
System.out.println("What is the name of your Log: ");
name = in.next();
ArrayIntLog myLog = new ArrayIntLog(name);
for (int index = 0; index <= myLog.size(); index++)
{
myLog.insert(12);
int duplicateCheck = log[index];
if (myLog.contains(duplicateCheck))
{
myLog.toString();
System.out.println("It took " + cycles + " cycles to generate duplicate numbers randomly.");
}
else
{
cycles++;
}
}
}
}
这里是我的数组INT日志类:
package arrayintlog;
public class ArrayIntLog implements IntLogInterface
{
protected String name; //name of the IntLog
protected int[] log; //array that holds the integers
protected int lastIndex = -1;
//==========================Constructor=====================================
public ArrayIntLog(String name, int maxSize)
{
log = new int[maxSize];
this.name = name;
}
//==========================Constructor=====================================
public ArrayIntLog(String name)
{
log = new int[100];
this.name = name;
}
//===========================Insert=========================================
public void insert(int element)
{
lastIndex++;
log[lastIndex] = element;
}
//===========================isFull=========================================
public boolean isFull()
{
if(lastIndex == (log.length - 1))
{
return true;
}
else
{
return false;
}
}
//============================Size==========================================
public int size()
{
return lastIndex + 1;
}
//===========================Contains=======================================
public boolean contains(int element)
{
int location = 0;
while (location <= lastIndex)
{
if (element == (log[location]))
{
return true;
}
else
{
location++;
}
}
return false;
}
//=============================Clear========================================
public void clear()
{
for (int index = 0; index <= lastIndex; index++)
{
log[index] = null;
}
lastIndex = -1;
}
//=============================getName======================================
public String getName()
{
return name;
}
public String toString()
{
String logString = "Log " + name +"/n/n";
for (int index = 0; index <= lastIndex; index++)
{
logString = logString + (index+1) + ". " +
log[index] + "/n";
}
return logString;
}
}
好吧,我必须无意中在我的界面中使用了包含方法String而不是int。我赞赏完整的解释。对不起,我没有发布界面开始。我确信这不能包含这个问题......生活和学习 – twjohns29 2014-09-03 13:20:51