Topic: 请教帮忙 实现一个随机打印功能

  Print this page

1.请教帮忙 实现一个随机打印功能 Copy to clipboard
Posted by: jc841020245
Posted on: 2006-11-13 23:46

随机在屏幕上打印1---60不可以重复打印

请高手帮忙
谢谢

2.Re:请教帮忙 实现一个随机打印功能 [Re: jc841020245] Copy to clipboard
Posted by: dracularking
Posted on: 2006-11-14 00:40

import java.util.Random;

public class Test
{
private static final int MAX=60;

public static void main(String[] args)
{
Test test=new Test();
Random random=new Random(System.currentTimeMillis());
int[] array=new int[MAX];
int currentNumber=0,count=0;

while(true)
{
currentNumber=Math.abs(random.nextInt())%MAX+1;
if(test.isRepeat(currentNumber,array,count))
{
continue;
}
else
{
array[count]=currentNumber;
count++;
}

if(count>=MAX)
{
break;
}
}

test.printNumber(array);
}

private boolean isRepeat(int currentNumber,int[] array,int count)
{
for(int i=0;i<count;i++)
{
if(currentNumber==array[i])
{
return true;
}
}
return false;
}

private void printNumber(int[] array)
{
for(int i=0;i<MAX;i++)
{
System.out.print(array[i]+" ");
}
}
}

3.Re:请教帮忙 实现一个随机打印功能 [Re: jc841020245] Copy to clipboard
Posted by: zhangwensheng
Posted on: 2006-11-14 09:38

import java.util.*;
class test{
public static void main(String str[])
{
int[] num = new int[60];
int i = 0, j = 0;
Random rd = new Random();
System.out.println("生成的随机数:");
for(i = 0; i < 60; i++)
{
do
{
num[i] = Math.abs(rd.nextInt())%60 + 1;
for(j = 0; j < i; j++)
{
if(num[j] == num[i])
{
break;
}
}
if(j == i)
{
break;
}
} while(true);
System.out.print(num[i] + " ");
}
System.out.println("\n排序后:");
for(i = 0; i < 60 - 1; i++)
{
for(j = i + 1; j < 60; j++)
{
if(num[i] > num[j])
{
num[i] = num[i] + num[j];
num[j] = num[i] - num[j];
num[i] = num[i] - num[j];
}
}
}
for(i = 0; i < 60; i++)
{
System.out.print(num[i] + " ");
}
}
}

4.Re:请教帮忙 实现一个随机打印功能 [Re: dracularking] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2006-11-14 17:37

dracularking wrote:
import java.util.Random;

public class Test
{
private static final int MAX=60;

public static void main(String[] args)
{
Test test=new Test();
Random random=new Random(System.currentTimeMillis());
int[] array=new int[MAX];
int currentNumber=0,count=0;

while(true)
{
currentNumber=Math.abs(random.nextInt())%MAX+1;
if(test.isRepeat(currentNumber,array,count))
{
continue;
}
else
{
array[count]=currentNumber;
count++;
}

if(count>=MAX)
{
break;
}
}

test.printNumber(array);
}

private boolean isRepeat(int currentNumber,int[] array,int count)
{
for(int i=0;i<count;i++)
{
if(currentNumber==array[i])
{
return true;
}
}
return false;
}

private void printNumber(int[] array)
{
for(int i=0;i<MAX;i++)
{
System.out.print(array[i]+" ");
}
}
}



excellent.

5.Re:请教帮忙 实现一个随机打印功能 [Re: jc841020245] Copy to clipboard
Posted by: jc841020245
Posted on: 2006-11-21 09:40

多谢高手
感激不尽


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923