所以我是第一年的科學Sci學生,我必須創建一個程序,它會將一個數組加擾,然後重新排序。我幾乎可以正常工作,但由於某種原因,我的一個while循環不起作用,並且完全被繞過。我真的很新,所以所有的幫助表示讚賞。我已經排序,打印工作,爭奪只是給我一些麻煩。雖然環路被打翻?
import java.io.*;
import java.util.Random;
public class Driver03
{
public static void main(String[] args)
{
int[] array = {100, 101, 102, 103, 104, 105, 106, 107, 108, 109};
print(array);
array = scramble(array);
print(array);
array = sort(array);
print(array);
}
public static void print(int[] apple)
{
for(int x = 0; x < apple.length; x++){
System.out.println(apple[x]+"");
}
}
public static int max;
public static int maxIndex;
public static int temp;
public static int[] sort(int[] grape)
{
for(int y = grape.length; y > 0; y--){
for(int x = 0; x < y; x++)
if (x==1){
if (grape[x] > grape[x-1]){
max = grape[x];
maxIndex = x;
}
else{
max = grape[x-1];
maxIndex = x - 1;
}
}
else{
if (grape[x] > max){
max = grape[x];
maxIndex = x;
}
}
temp = grape[maxIndex];
grape[maxIndex] = grape[y-1];
grape[y-1] = temp;
}
return grape;
}
public static int temp1;
public static boolean t;
public static boolean u;
public static int[] numbers;
public static int[] tangerine;
public static int[] scramble(int[] orange)
{
numbers = new int[10];
tangerine = new int[10];
Random rand=new Random();
for(int x = 0; x < orange.length; x++){
t=false;
while (t=false){ //For some reason being bypassed
int randn = rand.nextInt(9);
for(int y = 0; y < numbers.length; y++){
if (numbers[x]==randn)
u = true;
}
if (! (u==true))
{
numbers[randn] = randn;
tangerine[randn] = orange[x];
t = true;
}
}
}
return tangerine;
}
}
我也猜測你沒有在IDE中工作。例如,Eclipse會警告你這樣的錯誤。 – OrangeDog