所以我是一年级的comp 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;
}
}