Java. How to generate what's left

I am working on a program that will generate a random month and year, and then, when this happens, it will show which months are left, but only this year, for example, in 2016, January appears so that it prints February-December 2016. ve created a random generation, but I don't know how to create a string to show what is left.

public class months {

    public static void main(String[] args) {
        String[] years = {
                "2017", "2016", "2015", "2104"
        };

        String[] months = {
                "Januay", "Febuary", "March", "April", "May", "June", "july", "August"
                , "september", "october"};


        }
        // initialize months

        int i = (int) (Math.random() * months.length);
        int j = (int) (Math.random() * years.length);
        System.out.println(months[i] + " of " + years[j]);
        System.out.println("The remaining months left in that suit are:");

        //print out rest of remaining months in that year
    }
}

Any hints or tips would be appreciated or there could be links where I could search. Brand new for java and just playing.

+4
source share
1 answer

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.

// Choose random month and random year.
int monthIndex = java.util.concurrent.ThreadLocalRandom.current( ).nextInt( 0 , months.length ); // ( inclusive , exclusive )
int yearIndex = java.util.concurrent.ThreadLocalRandom.current( ).nextInt( 0 , years.length ); // ( inclusive , exclusive )
System.out.println( months[ monthIndex ] + " of " + years[ yearIndex ] );

. months, for, , .

, , 11, . . .

System.out.println( "The remaining months left in that suit are:" );
//print out rest of remaining months in that year

if ( monthIndex == months.length )
{
    System.out.println( "No more months remaining after " + months[ monthIndex ] + " " + years[ yearIndex ] );
} else
{
    // Loop through each of the remaining months.
    // Strarting with first month *after* the chosen month, so adding one to the starting index.
    for ( int m = ( monthIndex + 1 ) ; m < months.length ; m++ )
    {
        System.out.println( months[ m ] + " of year " + years[ yearIndex ] );  // Print chosen year with each remaining month.
    }
}

2017

, , :

2017

2017


, , .

Year , .

Month enum , .

, YearMonth.

Set EnumSet , List/ArrayList, .

// YEAR ***************
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( ) ); // ( inclusive , exclusive )
Year year = years.get( yearIndex );

// MONTH ***********
Set < Month > months = EnumSet.allOf( Month.class );  // EnumSet should be marked as implementing `SortedSet` but mysteriously is not. It does return its always in a particular order,  in which defined in the Enum. So our months here will be ordered Jan-Dec as we expect.
int monthIndex = ThreadLocalRandom.current( ).nextInt( 0 , months.size( ) ); // ( inclusive , exclusive )
Month month = new ArrayList <>( months ).get( monthIndex );  // Make a `List` from `Set` to be able to conveniently pick a certain one by index number.

// YEAR-MONTH *************
YearMonth chosen = YearMonth.of( year.getValue() , month );
System.out.println("chosen: " + chosen );

// REMAINING ************
// Remaining year-month values to finish out the chosen year after the chosen month.
List < YearMonth > yearMonths = new ArrayList <>( months.size( ) - monthIndex );  // Collect results of our report generation.
Set < Month > remainingMonths = EnumSet.range( month , Month.DECEMBER );
for ( Month m : remainingMonths )
{
    // Skip first month, as we want only the months *after* the chosen month.
    if ( m.equals( month ) )
    {
        // Skip. No code needed here.
    } else
    {
        yearMonths.add( YearMonth.of( year.getValue( ) , m ) );  // Instantiate a YearMonth object from our chosen year number and this nth remaining month of year.
    }
}

// REPORT ******************
System.out.println( "yearMonths: " + yearMonths );

// Pretty-print the name of the month in English along with year number.
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

+3

Source: https://habr.com/ru/post/1688166/


All Articles