日期:2014-05-20  浏览次数:20667 次

java编程思想书里面的练习,求解!!谢谢
练习1:写一个程序,产生25个int类型的随机数。对于每一个随机值,使用if-else语句来将其分类为大于、小于,或等于紧随它而随机生成的值?
我刚学习,希望能给我解答!!谢谢


如果谁有这本书的练习文档答案,给我发一份《java编程思想   第4版》

------解决方案--------------------
import java.util.*;
public class Test4 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Random r = new Random();
ArrayList bigArray = new ArrayList();
ArrayList smallArray = new ArrayList();
ArrayList equalArray = new ArrayList();
ArrayList array = new ArrayList();
int range = 100;
int privious = r.nextInt(range);
array.add(privious);
int current;
int n = 25;
for(int i=1; i <n; i++) {
current = r.nextInt(range);
array.add(current);
if(privious > current) bigArray.add(privious);
else if(privious < current) smallArray.add(privious);
else equalArray.add(privious);
privious = current;
}
System.out.println(array);
System.out.println(bigArray);
System.out.println(smallArray);
System.out.println(equalArray);
}
}
------解决方案--------------------
集合类要使用范型的,你没有使用范型所以会有警告.这样就没有了.
import java.util.*;
public class Test4 {

public static void main(String[] args) {
Random r = new Random();
ArrayList <Integer> bigArray = new ArrayList <Integer> ();
ArrayList <Integer> smallArray = new ArrayList <Integer> ();
ArrayList <Integer> equalArray = new ArrayList <Integer> ();
ArrayList <Integer> array = new ArrayList <Integer> ();
int range = 100;
int privious = r.nextInt(range);
array.add(privious);
int current;
int n = 25;
for(int i=1; i <n; i++) {
current = r.nextInt(range);
array.add(current);
if(privious > current) bigArray.add(privious);
else if(privious < current) smallArray.add(privious);
else equalArray.add(privious);
privious = current;
}
System.out.println(array);
System.out.println(bigArray);
System.out.println(smallArray);
System.out.println(equalArray);
}
}