Primitives and Arrays
Monthly cycle starting from the selected month
. , 1
.
, , ( Nov-Dec).
String[] years = {
"2017" , "2016" , "2015" , "2104"
};
String[] months = {
"Januay" , "Febuary" , "March" , "April" , "May" , "June" , "july" , "August" , "september" , "october" , "November" , "December" };
int n = years.length * months.length;
String[] deck = new String[ n ];
for ( int i = 0 ; i < months.length ; i++ )
{
for ( int j = 0 ; j < years.length ; j++ )
{
deck[ years.length * i + j ] = months[ i ] + " of " + years[ j ];
}
}
, ThreadLocalRandom
, Math.Random
, , . , , JavaDoc.
int monthIndex = java.util.concurrent.ThreadLocalRandom.current( ).nextInt( 0 , months.length );
int yearIndex = java.util.concurrent.ThreadLocalRandom.current( ).nextInt( 0 , years.length );
System.out.println( months[ monthIndex ] + " of " + years[ yearIndex ] );
. months
, for
, , .
, , 11, . . .
System.out.println( "The remaining months left in that suit are:" );
if ( monthIndex == months.length )
{
System.out.println( "No more months remaining after " + months[ monthIndex ] + " " + years[ yearIndex ] );
} else
{
for ( int m = ( monthIndex + 1 ) ; m < months.length ; m++ )
{
System.out.println( months[ m ] + " of year " + years[ yearIndex ] );
}
}
2017
, , :
2017
2017
, , .
Year
, .
Month
enum , .
, YearMonth
.
Set
EnumSet
, List
/ArrayList
, .
List < Year > years = new ArrayList <>( 4 );
years.add( Year.of( 2017 ) );
years.add( Year.of( 2016 ) );
years.add( Year.of( 2015 ) );
years.add( Year.of( 2014 ) );
int yearIndex = ThreadLocalRandom.current( ).nextInt( 0 , years.size( ) );
Year year = years.get( yearIndex );
Set < Month > months = EnumSet.allOf( Month.class );
int monthIndex = ThreadLocalRandom.current( ).nextInt( 0 , months.size( ) );
Month month = new ArrayList <>( months ).get( monthIndex );
YearMonth chosen = YearMonth.of( year.getValue() , month );
System.out.println("chosen: " + chosen );
List < YearMonth > yearMonths = new ArrayList <>( months.size( ) - monthIndex );
Set < Month > remainingMonths = EnumSet.range( month , Month.DECEMBER );
for ( Month m : remainingMonths )
{
if ( m.equals( month ) )
{
} else
{
yearMonths.add( YearMonth.of( year.getValue( ) , m ) );
}
}
System.out.println( "yearMonths: " + yearMonths );
for ( YearMonth ym : yearMonths )
{
System.out.println( ym.getMonth( ).getDisplayName( TextStyle.FULL , Locale.US ) + " of " + ym.getYear( ) );
}
: 2016-10
yearMonths: [2016-11, 2016-12]
2016
2016