✔ 最佳答案
try following:
public class GenNum
{
public static void main(String[] args)
{ int iMin = 1;
int iMax = 20;
int iTeam = 5;
int iNum[ ] = iGenNum( iMin, iMax, iTeam, false );
for ( int i = 0; i < iTeam; i++ )
System.out.println( String.valueOf( iNum[ i ] ) );
}
public static int [ ] iGenNum( int piMin, int piMax,
int piTeam, boolean pbRepeat )
{ int iRange = piMax - piMin;
int iReturn[ ] = new int [ iRange ];
int iPtr = 0;
int iPtr1 = 0;
for ( iPtr = 0; iPtr < piTeam; )
{
iReturn[ iPtr ] = ( int ) Math.floor( ( iRange + 1 ) * Math.random( ) ) + piMin;
if ( ! pbRepeat )
{ if ( iPtr > 0 )
{ for ( iPtr1 = 0; iPtr1 < iPtr; iPtr1++ )
if ( iReturn[ iPtr ] == iReturn[ iPtr1 ] )
iPtr--;
}
}
iPtr++;
}
return iReturn;
}
}
for the function iGenNum
piMin : integer parameter to set the minimum generated value.
piMax: integer parameter to set the maximum generated value.
piTeam: integer parameter to set how many team generate.
pbRepeat: boolean parameter to check the generated number can repeat or not.
assumption:
in function iGenNum, the parameter piTeam would not be large than the value of ( piMax - piMin ).
OK?