使用java.lang.Math类,生成100个0到99之间的随机整数,找出它们之中的最大者和最小者,并统计大于50的整数个数
Java代码 <embed height="15" width="14" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="always" quality="high" flashvars="clipboard=package%20test01%3B%0Apublic%20class%20RandomNum%20%7B%0A%0A%09public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%09%09int%20number%5B%5D%20%3D%20new%20int%5B100%5D%3B%0A%09%09int%20record%20%3D%200%3B%0A%09%09for%20(int%20i%20%3D%200%3B%20i%20%3C%20number.length%3B%20i%2B%2B)%20%7B%0A%09%09%09number%5Bi%5D%20%3D%20(int)%20Math.round(99%20*%20Math.random())%3B%0A%09%09%09if%20(number%5Bi%5D%20%3E%2050)%20%7B%0A%09%09%09%09record%2B%2B%3B%0A%09%09%09%7D%0A%09%09%7D%0A%09%09System.out.println(%22%E5%A4%A7%E4%BA%8E50%E7%9A%84%E6%95%B4%E6%95%B0%E6%9C%89%3A%22%20%2B%20record%20%2B%20%22%E4%B8%AA%22)%3B%0A%09%09%2F%2F%20%E7%BB%99%E6%95%B0%E7%BB%84%E6%8E%92%E5%BA%8F%EF%BC%88%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F%EF%BC%89%0A%09%09for%20(int%20i%20%3D%200%3B%20i%20%3C%20number.length%3B%20i%2B%2B)%20%7B%0A%09%09%09for%20(int%20j%20%3D%200%3B%20j%20%3C%20number.length%20-%20i%20-%201%3B%20j%2B%2B)%20%7B%0A%09%09%09%09if%20(number%5Bj%5D%20%3C%20number%5Bj%20%2B%201%5D)%20%7B%0A%09%09%09%09%09int%20temp%20%3D%20number%5Bj%5D%3B%0A%09%09%09%09%09number%5Bj%5D%20%3D%20number%5Bj%20%2B%201%5D%3B%0A%09%09%09%09%09number%5Bj%20%2B%201%5D%20%3D%20temp%3B%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09System.out.println(%22%E6%9C%80%E5%A4%A7%E7%9A%84%E6%95%B4%E6%95%B0%3A%22%20%2B%20number%5B0%5D)%3B%0A%09%09System.out.println(%22%E6%9C%80%E5%B0%8F%E6%95%B4%E6%95%B0%22%20%2B%20number%5B99%5D)%3B%0A%09%7D%0A%7D" src="http://jythoner.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_mediaid="lk_juiceapp_mediaPopup_1234236934071" lk_media="yes">
- package test01;
- public class RandomNum {
- public static void main(String[] args) {
- int number[] = new int[100];
- int record = 0;
- for (int i = 0; i < number.length; i++) {
- number[i] = (int) Math.round(99 * Math.random());
- if (number[i] > 50) {
- record++;
- }
- }
- System.out.println("大于50的整数有:" + record + "个");
- // 给数组排序(冒泡排序)
- for (int i = 0; i < number.length; i++) {
- for (int j = 0; j < number.length - i - 1; j++) {
- if (number[j] < number[j + 1]) {
- int temp = number[j];
- number[j] = number[j + 1];
- number[j + 1] = temp;
- }
- }
- }
- System.out.println("最大的整数:" + number[0]);
- System.out.println("最小整数" + number[99]);
- }
- }
或者
Java代码 <embed height="15" width="14" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="always" quality="high" flashvars="clipboard=package%20test01%3B%0Apublic%20class%20RandomNum2%20%7B%0A%0A%09public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%09%09int%20min%20%3D%2099%3B%0A%09%09int%20max%20%3D%200%3B%0A%09%09int%20temp%3B%0A%09%09int%20count%20%3D%200%3B%0A%09%09for%20(int%20i%20%3D%200%3B%20i%20%3C%2010%3B%20i%2B%2B)%20%7B%0A%09%09%09temp%20%3D%20(int)%20(Math.random()%20*%20100)%3B%0A%09%09%09%2F%2F%20System.out.println(temp)%3B%0A%09%09%09if%20(temp%20%3E%2050)%20%7B%0A%09%09%09%09count%2B%2B%3B%0A%09%09%09%7D%0A%0A%09%09%09if%20(min%20%3E%20temp)%20%7B%0A%09%09%09%09min%20%3D%20temp%3B%0A%09%09%09%7D%0A%0A%09%09%09if%20(max%20%3C%20temp)%20%7B%0A%09%09%09%09max%20%3D%20temp%3B%0A%09%09%09%7D%0A%09%09%7D%0A%09%09System.out.println(%22Max%20is%3A%22%20%2B%20max)%3B%0A%09%09System.out.println(%22Min%20is%3A%22%20%2B%20min)%3B%0A%09%09System.out.println(%22The%20count%20that%20bigger%20than%2050%20is%3A%22%20%2B%20count)%3B%0A%09%7D%0A%7D" src="http://jythoner.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_mediaid="lk_juiceapp_mediaPopup_1234236934076" lk_media="yes">
- package test01;
- public class RandomNum2 {
- public static void main(String[] args) {
- int min = 99;
- int max = 0;
- int temp;
- int count = 0;
- for (int i = 0; i < 10; i++) {
- temp = (int) (Math.random() * 100);
- // System.out.println(temp);
- if (temp > 50) {
- count++;
- }
- if (min > temp) {
- min = temp;
- }
- if (max < temp) {
- max = temp;
- }
- }
- System.out.println("Max is:" + max);
- System.out.println("Min is:" + min);
- System.out.println("The count that bigger than 50 is:" + count);
- }
- }