首页上一页 1 下一页尾页 1 条记录 1/1页
获取随机6个偶数,并求和。答案给出的多了很多代码,无法理解。
发表在Java图书答疑
2013-03-02
是否精华
是
否
版块置顶:
是
否
[font color=#00FF00]这是答案给出的代码[/font]:
package com.lzw;
import java.util.*;
public class RandomTest {
/**
* @param args
* 获取2到32之间的6个偶数,并且求这6个偶数的和
*/
public static double EvenNum(double num1,double num2){
double s=0;
int[] j=new int[6];
Random cres = new Random();
for (int w = (int)num1,i=0; w <= (int)num2; w += 2) {
j[i] = cres.nextInt((int) num2);
if (j[i]!=0&&j[i] % 2 == 0) {
System.out.println(j[i]);
s += j[i];
i++;
}
if(i==6) break;
}
return s;
}
[font color=#FF0000]public static double EvenNum2(double num1,double num2){
double s=0;
int[] j=new int[6];
for(int i=0,w=0;i<20;i++){
j[w]=(int)num1+(int)(Math.random()*(num2-num1));
if (j[w]!=0&&j[w] % 2 == 0) {
System.out.println("打印2到32之间的偶数(不包括32)"+j[w]);
s += j[w];
w++;
}
if(w==6) break;
}
return s;
}
public static void main(String[] args) {
System.out.println("和为:"+EvenNum2(2,32));
}[/font]
}
明明红色部分的字可以实现操作了。 红色之前那一段代码思路是什么啊,。我看不懂。、为什么要多这么一段代码。意义何在。