我正在研究一個程序,它會將隨機數添加到數組中,直到它將一個重複數字放到數組中,然後它應該打印生成重複數字之前生成了多少數字。爲什麼我收到此運行時異常?
當我運行該程序時,我收到此錯誤。
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