Help, please, solve this problem in Java. It is not entirely clear how to implement it through a switch.

Task:

The Eastern calendar adopted a 60-year cycle consisting of 12-year subcycles, denoted by the names of colors: green, red, yellow, white and blue. In each sub-cycle years, the names of animals are rat, cow, tiger, hare, dragon, snake, horse, sheep, monkey, chicken, dog, and pig. By the number of the year to determine its name, if 1984 is the beginning of the cycle: “the year of the green rat”.

Examples:

Введите год: 2012 Ответ: дракон, голубой Введите год: 1956 Ответ: обезьяна, красный 
  • switch is not needed here ... Better array of values. - pavel
  • @pavel is certainly better array, but the task should be just switch ((unfortunately - Diq
  • In the first step, on the basis of the year entered, determine its place from the beginning of the 60 year cycle (for example, 5 of 60 years). In the second step in the switch, select the period in which this year falls. And no array is needed - Viktorov

2 answers 2

If you need to switch through something like this:

 int year = 2016; int deltaYear = year % 60; //откинем все прошедшие циклы int colorYear = deltaYear % 12; // какой сейчас цвет String color; switch(colorYear){ //кейсы по цветам // color = "цвет"; } int animalYear = deltaYear / 12; // какое сейчас животное String animal; switch(animalYear ){ //кейсы по животным // animal = "животное"; } return animal + ", " + color; 

But I agree that it is easier to get arrays with color and animals and just pull out on the index.

    Step 1: Based on the year, determine its position in the 60 year cycle.

    For example, the year 1998 is entered, its position will be 14.

    Step 2: Determine which interval it is in

    About

      Если меньше 12 то зеленый Если меньше 24 то красный Если меньше 36 то желтый ... 

    For the example above, it will be red.

    Step 3: Take the remainder of the division by 12, and get the position of the year in the 12 year cycle Approximately:

      Если 1 то крыса Если 2 то корова Если 3 то тигр ... 

    PS
    Consciously did not write the code, since this is a learning task, and at least you should write the code yourself