Reputation: 11
I signed up today and this is the first thread I open.
I have recently started studying the Kotlin language, taking a course on Udemy, but i’m still at the beginning.
I have produced a small script that allows you to know your zodiac sign according to the Chinese horoscope, entering the year of birth. Here the script:
fun main(){
println("Enter your year of birth")
val yearOfBirth = readln().toInt()
when (yearOfBirth) {
2020, 2008, 1996, 1984, 1972, 1960, 1948, 1936 -> println("Your Chinese zodiac sign is: Rat")
2021, 2009, 1997, 1985, 1973, 1961, 1949, 1937 -> println("Your Chinese zodiac sign is: Buffalo")
2022, 2010, 1998, 1986, 1974, 1962, 1950, 1938 -> println("Your Chinese zodiac sign is: Tiger")
2023, 2011, 1999, 1987, 1975, 1963, 1951, 1939 -> println("Your Chinese zodiac sign is: Rabbit")
2024, 2012, 2000, 1988, 1976, 1964, 1952, 1940 -> println("Your Chinese zodiac sign is: Dragon")
2025, 2013, 2001, 1989, 1977, 1965, 1953, 1941 -> println("Your Chinese zodiac sign is: Snake")
2026, 2014, 2002, 1990, 1978, 1966, 1954, 1942 -> println("Your Chinese zodiac sign is: Horse")
2027, 2015, 2003, 1991, 1979, 1967, 1955, 1943 -> println("Your Chinese zodiac sign is: Goat")
2028, 2016, 2004, 1992, 1980, 1968, 1956, 1944 -> println("Your Chinese zodiac sign is: Monkey")
2029, 2017, 2005, 1993, 1981, 1969, 1957, 1945 -> println("Your Chinese zodiac sign is: Chiken")
2030, 2018, 2006, 1994, 1982, 1970, 1958, 1946 -> println("Your Chinese zodiac sign is: Dog")
2031, 2019, 2007, 1995, 1983, 1971, 1959, 1947 -> println("Your Chinese zodiac sign is: Pig")
else -> println("That's something wrong...")
}
}
What I would like to do now, is create one that works for any year.
To do this you will need: an array of 12 indexes with the name of the relative sign a function that correctly calculates the index based on the required year (since the same 12-year cycles always follow one another) At that point, the output does not have any conditions (when), but only the output of the string associated with the correct index.
Bonus: once the year has been solved, also add the management of the month and the day (so for those born on January 15 it does not get the wrong sign, for example).
This is a first proof:
fun main(){
var sign = arrayOf("Rat", "Buffalo", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Goat", "Monkey", "Chicken", "Dog", "Pig")
println("Enter your year of birth")
val yearOfBirth = readln().toInt()
when (yearOfBirth) {
2020, 2008, 1996, 1984, 1972, 1960, 1948, 1936 -> println("Your Chinese zodiac sign is: Rat")
2021, 2009, 1997, 1985, 1973, 1961, 1949, 1937 -> println("Your Chinese zodiac sign is: Buffalo")
2022, 2010, 1998, 1986, 1974, 1962, 1950, 1938 -> println("Your Chinese zodiac sign is: Tiger")
2023, 2011, 1999, 1987, 1975, 1963, 1951, 1939 -> println("Your Chinese zodiac sign is: Rabbit")
2024, 2012, 2000, 1988, 1976, 1964, 1952, 1940 -> println("Your Chinese zodiac sign is: Dragon")
2025, 2013, 2001, 1989, 1977, 1965, 1953, 1941 -> println("Your Chinese zodiac sign is: Snake")
2026, 2014, 2002, 1990, 1978, 1966, 1954, 1942 -> println("Your Chinese zodiac sign is: Horse")
2027, 2015, 2003, 1991, 1979, 1967, 1955, 1943 -> println("Your Chinese zodiac sign is: Goat")
2028, 2016, 2004, 1992, 1980, 1968, 1956, 1944 -> println("Your Chinese zodiac sign is: Monkey")
2029, 2017, 2005, 1993, 1981, 1969, 1957, 1945 -> println("Your Chinese zodiac sign is: Chiken")
2030, 2018, 2006, 1994, 1982, 1970, 1958, 1946 -> println("Your Chinese zodiac sign is: Dog")
2031, 2019, 2007, 1995, 1983, 1971, 1959, 1947 -> println("Your Chinese zodiac sign is: Pig")
else -> println("That's something wrong...")
}
}
It works, but it's not how I want it to be, as explained above.
Thank you so mutch.
Upvotes: 1
Views: 34
Reputation: 11317
A couple of minor tips:
I suggest you change your array name to signs
. Array names conventionally are plural. The array signs
contains 12 signs. A single element of signs
is just one sign.
Use val
instead of var
, unless you really need to change the value of a variable after you set it.
Notice the following pattern for the zodiac signs:
signs[0]
.signs[1]
.signs[11]
.So, for the years 1936 to 1947, you can do this:
println("Your Chinese zodiac sign is: " + signs[yearOfBirth - 1936])
or, if you've learned about string templates, you can do this:
println("Your Chinese zodiac sign is: ${signs[yearOfBirth - 1936]}")
Now, what about those born more recently? The pattern is as follows:
signs[0]
.signs[1]
.These numbers are (yearOfBirth - 1936) modulo 12
- the remainder operator is %
, so the final version is:
fun main() {
val signs = arrayOf("Rat", "Buffalo", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Goat", "Monkey", "Chicken", "Dog", "Pig")
println("Enter your year of birth")
val yearOfBirth = readln().toInt()
val index = (yearOfBirth - 1936) % 12
val sign = signs[index]
println("Your Chinese zodiac sign is: $sign")
}
Note: as tyg says in the comment, this solution can only handle years from 1936 onwards. I've given this solution as a direct equivalent to the OP's code, which also doesn't support years before 1936. If you do want to support earlier years, use the mod
function instead of the rem
operator (%
):
val index = (yearOfBirth - 1936).mod(12)
The difference between mod
and %
is that x.mod(12)
will always result in a number between 0 and 11, and will never be negative. For the year 1935, it will result in 11, which is what you'd want.
Upvotes: 3