2016-03-21 119 views
-1

我在这个论坛上搜索有人遇到类似于我的问题,我无法找到为什么地球上我的程序不能正常工作,除了看起来坚实。我刚刚开始学习Java,所以我不确定这里是否有某种语法错误,或者更多,但我一直在盯着它,而这个网站在过去的一个小时里,我没有看到怎么了。阵列没有正确初始化

public class BubbleSort { 

    public static void main(String[] args) { 
     int a, b, n, change, i, operations, choice; 
     Scanner input = new Scanner(System.in); 
     System.out.println("Would you like to add your own numbers?"); 
     System.out.println("enter '1' for no and '2' for yes"); 
     choice = input.nextInt(); 

     if (choice == 1) { 
      int[] array = { 
       5, 
       7, 
       3, 
       9, 
       1, 
       0, 
       6 
      }; 
      n = 7; 
     } else if (choice == 2) { 

      System.out.println("How many numbers would you like to add to the" + 
       "Array? (Add up to 10)"); 
      n = input.nextInt(); 

      int array[] = new int[n]; 

      System.out.println("Input " + n + " integers"); 

      for (a = 0; a < n; a++) { 
       array[a] = input.nextInt(); 
      } 
     } 

     System.out.println("Bubble Sort operation:"); 

     for (a = 0; a < (n - 1); a++) { 
      System.out.print("iteration " + (a + 1) + ": "); 

      for (b = 0; b < n - a - 1; b++) { 
       if (array[b] > array[b + 1]) { 

        change = array[b]; 
        array[b] = array[b + 1]; 
        array[b + 1] = change; 
        operations++; 

       } 
      } 
      for (i = 0; i < n; i++) { 
       System.out.print(array[i]); 
      } 
      System.out.println(); 
     } 

     System.out.print("Finished array after bubble sort: "); 
     for (i = 0; i < n; i++) { 
      System.out.print(array[i]); 
     } 
     System.out.println(); 
     System.out.println("This operation took " + operations + " cycles"); 
     System.out.println(); 
    } 
} 

这只是一个简单的泡沫排序程序,但我在这里严重难住。任何想法会有什么错误?错误说n从不初始化。

+0

你得到的错误是什么? – haihui

+0

这是说n没有被初始化 –

+0

另外,它没有认识到循环中的数组与if语句中初始化的数组相同 –

回答

2

Java编译器告诉你这个,因为变量n可能未被初始化。

“但是我做了!我写了n = 7;n = input.nextInt();!这不是叫'初始化'吗?”你问。你完全正确!你在这些地方初始化了n。但是,根据您的代码,n仅在choice为1或2时才会初始化。如果choice为3,该怎么办?或-999?如果是这样的话,n将不会被初始化,对吗?

比方说choice是-999。这两个if语句将被跳过,并直接执行到冒泡排序部分。被执行后,谈到这个代码:

System.out.print("Finished array after bubble sort: "); 
    for (i = 0; i < n; i++) { 
     System.out.print(array[i]); 
    } 
    System.out.println(); 
    System.out.println("This operation took " + operations + " cycles"); 
    System.out.println(); 

和它说,

Waaaaait一分钟,对于环说,我需要n比较i决定循环是否应该继续被执行。但我甚至不知道什么是价值!

这就是为什么它抱怨这一点。

如何来解决这个问题:

这取决于你是否把其他号码为无效的输入。如果你这样做,你可以写一个else子句的if语句

else { 
    System.out.println("Input Invalid. Program will now exit."); 
    return; 
} 

如果你想,就好像用户已经进入2对待其他数字,改变else if子句人。或者你可以只写

n = 0; 

在开头。

此外,我注意到你有另一个错误。在if语句,你写了这个:

int[] array = { 
    5, 
    7, 
    3, 
    9, 
    1, 
    0, 
    6 
}; 

这:

int array[] = new int[n]; 

这两个数组不会if语句外部访问。我认为你只需要在if语句之外声明一个数组,如下所示:

int[] array = { 
    5, 
    7, 
    3, 
    9, 
    1, 
    0, 
    6 
}; 
if (choice == 1) { 
    // The original declaration should be removed 
    n = 7; 
} else { 
    // ... 
} 
+0

感谢您的输入!我把n = 0,然后刷新程序,它的工作。然后我回到这里说了些什么,我看到你赞扬了它!哈哈。如果数字不是1或2,我还打算在该部分添加关于退出的部分。谢谢!现在我只需要弄清楚为什么程序不会像我选择选项1时那样喜欢我的数组。我不想像数组[0] = 5 array [1] =等等等等等等。 java中的列表/数组与python相比是一场噩梦......而且你比我更快!非常感谢您的帮助,您可以在这里保存我的成绩 –