我刚刚开始Java编程。我正在编写挑战书中的一些问题。问题是打印出给定整数集的最大循环长度。这是我的代码:
public class JavaApplication9 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("This is a program that calculates the maximum"
+ " cyclelength of two integers between 1 and 1000000");
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the first number: ");
int x = keyboard.nextInt();
System.out.println();
System.out.print("Enter the second number: ");
int y = keyboard.nextInt();
@SuppressWarnings("UseOfObsoleteCollectionType")
Vector<Integer> cycleVec = new Vector<Integer>();
Vector newVec = new Vector();
if (x <= 0 || y <= 0 || x >= 1000000 || y >= 1000000)
System.out.println("Wrong input. Input must be greater than 0"
+ " and less than 1,000,000!!");
else{
for(int k = Math.min(x, y);k<=Math.max(x, y);k++){
// We have to count 1 also
cycleVec.add(1 + cycleLength(k));
newVec.add(numList(k));
}
Enumeration vEnum = cycleVec.elements();
while(vEnum.hasMoreElements()){
int a = (int) vEnum.nextElement();
display(newVec, x, y, a);
}
System.out.println("\nElements in vector of cyclelengths:");
while(vEnum.hasMoreElements())
System.out.print(vEnum.nextElement() + " ");
System.out.println();
Object obj = Collections.max(cycleVec);
System.out.printf("%d %d ", x, y);
System.out.println(obj);
}
}
public static int cycleLength(int x){
int termPoint = 0;
int count = 0;
while (termPoint != 1){
if (x % 2 == 0){
x = x/2;
termPoint = x;
}else{
x = x*3 + 1;
termPoint = x;
}
++count;
}
return count;
}
public static Vector numList(int x){
int termPoint = 0;
Vector vec = new Vector();
while (termPoint != 1){
if (x % 2 == 0){
x = x/2;
termPoint = x;
}else{
x = x*3 + 1;
termPoint = x;
}
vec.addElement(x);
}
return vec;
}
public static void display(Vector v, int x, int y, int size){
System.out.println("Elements generated:");
int m = 0;
for(int k = Math.min(x, y); k <= Math.max(x, y); k++){
System.out.print("Number " + k + " ");
for (int i = 0; i < v.size(); i++){
for (int j = 0; j < size; j++){
m = (int)((Vector)v.elementAt(i)).elementAt(j);
System.out.print(m + " ");
}
System.out.println();
}
}
}
}
输出
run:
This is a program that calculates the maximum cyclelength of two integers between 1 and 1000000
Enter the first number: 201
Enter the second number: 210
Elements generated:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 18 >= 18
at java.util.Vector.elementAt(Vector.java:474)
Number 201604 302 151 454 227 682 341 1024 512 256 128 64 32 16 8 4 2 1 at javaapplication9.JavaApplication9.display(JavaApplication9.java:95)
at javaapplication9.JavaApplication9.main(JavaApplication9.java:44)
Java Result: 1
BUILD SUCCESSFUL (total time: 5 seconds)
代码的第一部分有效(如果删除显示功能)。我只想打印出每个整数x,生成的整数列表以及循环长度列表和最大循环长度。还有一种方法可以使这段代码更有效(减少代码行数)吗?
显示器的新代码(工作正常)
public static void display(Vector v){
System.out.println("Elements generated:");
int m = 0, w = 0;
for (int i = 0; i < v.size(); i++){
Vector inner = (Vector)v.elementAt(i);
System.out.println("Number ");
for (int j = 0; j < inner.size(); j++){
m = (int)inner.elementAt(j);
if (j == 0){
if (m % 2 == 0){
w = (m-1)/3;
}else{
w = m * 2;
}
System.out.print(w + " ");
}
System.out.print(m + " ");
}
System.out.println();
}
}
答案 0 :(得分:1)
display
方法的最后一个参数是一个整数,用作内部向量的大小,但是回到顶部我们看到它实际上比一行大一个:
// We have to count 1 also
cycleVec.add(1 + cycleLength(k));
您需要考虑cycleVec
值用于正确限制循环display
方法中的版本的内容。
异常意味着您尝试访问向量中的更多条目而不是它。您在同一行上有两次elementAt
来电。它看起来像是失败的第二个参考。这意味着j
太大,这意味着size
参数不正确。访问内部数组的安全方法如下:
for (int i = 0; i < v.size(); i++){
Vector inner = (Vector)v.elementAt(i);
for (int j = 0; j < inner.size(); j++) {
m = (int)inner.elementAt(j);
System.out.print(m + " ");
}
System.out.println();
}