用java 编写一个程序,要求要有一个循环和两个函数,必须要够50行
1个回答
展开全部
随便给你写了一个
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Test {
public static List<Integer> smallNumbers,largeNumbers;
public static int sum1 = 0,sum2 = 0;
public static void main(String[] args) {
smallNumbers = new ArrayList<Integer>();
largeNumbers = new ArrayList<Integer>();
run();
}
public static void run() {
int i = 0;
for (int j = 0; j < 50; j++) {
Random rand = new Random();
i = rand.nextInt(1000);
if (i<500) {
small(i);
}else{
large(i);
}
}
System.out.println("随机输出的50个数字中:");
System.out.println("大于500的数(包括500)共有"+largeNumbers.size()+"个");
System.out.print("他们是"+largeNumbers);
System.out.println();
System.out.println("他们的和是"+sum1);
System.out.println("小于500的数共有"+smallNumbers.size()+"个");
System.out.print("他们是"+smallNumbers);
System.out.println();
System.out.println("他们的和是"+sum2);
}
private static void large(int number) {
largeNumbers.add(number);
sum1 += number;
}
private static void small(int number) {
smallNumbers.add(number);
sum2 += number;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Test {
public static List<Integer> smallNumbers,largeNumbers;
public static int sum1 = 0,sum2 = 0;
public static void main(String[] args) {
smallNumbers = new ArrayList<Integer>();
largeNumbers = new ArrayList<Integer>();
run();
}
public static void run() {
int i = 0;
for (int j = 0; j < 50; j++) {
Random rand = new Random();
i = rand.nextInt(1000);
if (i<500) {
small(i);
}else{
large(i);
}
}
System.out.println("随机输出的50个数字中:");
System.out.println("大于500的数(包括500)共有"+largeNumbers.size()+"个");
System.out.print("他们是"+largeNumbers);
System.out.println();
System.out.println("他们的和是"+sum1);
System.out.println("小于500的数共有"+smallNumbers.size()+"个");
System.out.print("他们是"+smallNumbers);
System.out.println();
System.out.println("他们的和是"+sum2);
}
private static void large(int number) {
largeNumbers.add(number);
sum1 += number;
}
private static void small(int number) {
smallNumbers.add(number);
sum2 += number;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询