if (DateTime.Now <= 12){
Console.WriteLine("Good Morning")
}else if (DateTime.Now <= 16){
Console.WriteLine("Good Afternoon")
}else if (DateTime.Now <= 20){
Console.WriteLine("Good Evening")
}else {
Console.WriteLine("Good Night")
}
DateTime timeOfDayGreeting = DateTime.Now;
if (timeOfDayGreeting.Hour >= 5 && timeOfDayGreeting.Hour < 12)
{
Console.WriteLine("Good morning!");
}
else if (timeOfDayGreeting.Hour >= 12 && timeOfDayGreeting.Hour < 16)
{
Console.WriteLine("Good afternoon!");
}
else
{
Console.WriteLine("Good night!");
}
Console.ReadKey();
DateTime currentTime = DateTime.Now;
if (currentTime.Hour < 12 && currentTime.Hour >= 5)
{
Console.WriteLine("Good Morning");
}
else if (currentTime.Hour >= 12)
{
Console.WriteLine("Good Afternoon");
}
else if (currentTime.Hour >= 16 )
{
Console.WriteLine("Good Evening");
}
else
{
Console.WriteLine("Good Night");
}
Console.ReadKey();
that's great broThanks! I'd also just figured it out, while playing with a value I created with DateTime. I used the intellisense to literally scroll through everything!
This is what I did:
Code:DateTime timeOfDayGreeting = DateTime.Now; if (timeOfDayGreeting.Hour >= 5 && timeOfDayGreeting.Hour < 12) { Console.WriteLine("Good morning!"); } else if (timeOfDayGreeting.Hour >= 12 && timeOfDayGreeting.Hour < 16) { Console.WriteLine("Good afternoon!"); } else { Console.WriteLine("Good night!"); } Console.ReadKey();
Thanks for your input, though! OMG. I'm loving C#!
EDIT: Your code looks cleaner. I like your style!
Using your setup, I've come up with:
Code:DateTime currentTime = DateTime.Now; if (currentTime.Hour < 12 && currentTime.Hour >= 5) { Console.WriteLine("Good Morning"); } else if (currentTime.Hour >= 12) { Console.WriteLine("Good Afternoon"); } else if (currentTime.Hour >= 16 ) { Console.WriteLine("Good Evening"); } else { Console.WriteLine("Good Night"); } Console.ReadKey();
Python just added "switch case" statements and I hadn't even realized it. It happened in Python 3.10. They call them "match case" instead of "switch case", though.
fun interface TimeOfDay: (Int) -> Boolean
fun sayHi() {
val morning = TimeOfDay { it in 8..11 }
val afternoon = TimeOfDay { it in 12..15 }
val evening = TimeOfDay { it in 16..24 }
val we = object {}
infix fun LocalDateTime.`is`(check: (Int) -> Boolean) = check(hour)
infix fun Any.say(msg: String) = msg.also { println(it) }
infix fun Any.drink(what: String) = println("$what mmm yumii")
infix fun String.and(what: String) = apply { }
LocalDateTime.now().let {
when {
it `is` morning -> we say "Good Morning"
it `is` afternoon -> we say "Good Afternoon"
it `is` evening -> we say "Good Evening"
else -> we drink ("Coffee" and (we say "It's coding time"))
}
}
}
That looks so confusing...Better switch to Kotlin. It's fun
Not only we use "fun" keyword for function, but we also got fun interfaces
fun interface TimeOfDay: (Int) -> Boolean fun sayHi() { val morning = TimeOfDay { it in 8..11 } val afternoon = TimeOfDay { it in 12..15 } val evening = TimeOfDay { it in 16..24 } val we = object {} infix fun LocalDateTime.`is`(check: (Int) -> Boolean) = check(hour) infix fun Any.say(msg: String) = msg.also { println(it) } infix fun Any.drink(what: String) = println("$what mmm yumii") infix fun String.and(what: String) = apply { } LocalDateTime.now().let { when { it `is` morning -> we say "Good Morning" it `is` afternoon -> we say "Good Afternoon" it `is` evening -> we say "Good Evening" else -> we drink ("Coffee" and (we say "It's coding time")) } } }
Boy oh boy, if that feels tough, try reading some rustlang code.That looks so confusing...
Why do people create programming languages that just seem so.... overly complicated?
I think it makes them feel smarter.
From reading through the Rust documentation, most of what I've seen of it makes sense (data types, functions, control flow, etc), though admittedly, I've not yet gotten my hands dirty with it.Boy oh boy, if that feels tough, try reading some rustlang code.![]()
![]()
Hmmm it does make sense to use the “borrowing” concept (which is what scares me the most), but I find it really weird in practical use.From reading through the Rust documentation, most of what I've seen of it makes sense (data types, functions, control flow, etc), though admittedly, I've not yet gotten my hands dirty with it.
This Kotlin language, however. It just seems complicated for the sake of being complicated.
Rust is possibly on my todo list for 2023. It's either going to be Rust or Golang. I'm leaning more towards Rust, but haven't decided.Hmmm it does make sense to use the “borrowing” concept (which is what scares me the most), but I find it really weird in practical use.
Golang is pretty easy to read atleast. I would suggest going for both, preferably rustlang first, as it will take time to get used to. Golang, I can promise you will learn quickly.Rust is possibly on my todo list for 2023. It's either going to be Rust or Golang. I'm leaning more towards Rust, but haven't decided.
That looks so confusing...
that kotlin shit isnt overly complicated its just ugly as fuck.
Well that's good to know. Let's see the actual code for that example. Now I'm curious.That's not how we usually code. The idea was to create a line of code that says "When it is morning - we say good morning" with the least amount of brackets, like it ain't even written in a coding language, you guys missed the point
If I were to put here a usual example of how it should be done in kotlin that would be too boring!
Let's see the actual code
val currentTime = LocalDateTime.now()
val greetingMessage = when (currentTime.hour) {
in 8..11 -> "Good Morning"
in 12..15 -> "Good Afternoon"
in 16..24 -> "Good Evening"
else -> "Good Night"
}
println(greetingMessage)
You probably already noticed but, this code does not work as intended.Using your setup, I've come up with:
Code:DateTime currentTime = DateTime.Now; if (currentTime.Hour < 12 && currentTime.Hour >= 5) { Console.WriteLine("Good Morning"); } else if (currentTime.Hour >= 12) { Console.WriteLine("Good Afternoon"); } else if (currentTime.Hour >= 16 ) { Console.WriteLine("Good Evening"); } else { Console.WriteLine("Good Night"); } Console.ReadKey();
... currentTime.Hour >= 12 && currentTime.Hour < 17 ...