所以我是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;
}
}
Run Code Online (Sandbox Code Playgroud)
t=false
值分配false
给t
并评估新的价值.所以你刚刚写完了while (false)
.
比较您需要使用的相等性时==
.
然而,在布尔值的情况下,使用它的风格要好得多while (!t)
.由于缺少第二个,因此更具可读性且不太可能出错=
.
归档时间: |
|
查看次数: |
146 次 |
最近记录: |