How to Change From Within and Reignite The Fire Of Your Soul

One day somebody took you by the hand and told you to trust them. You were a little kid, you need help. Everybody was taller and smarter than you. So, you listened to them. Years passed by, you grew…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Getting Input

Programming Crystal — by Ivo Balbaert, Simon St. Laurent (38 / 125)

👈 Converting Data Between Types | TOC | Putting It Together — Converting Currencies 1 👉

You can make bad choices with data while you’re programming, but it’s even more likely that users will put incorrect data into your programs when asked for input. Creating resilient code means taking special care with user input. In the following section, your program reads input, and you’ll learn to make it withstand runtime errors.

The user provides the numbers, and you expect that they will be smaller than 256, so they’re of type Int8.

You’ll start with an empty array so you have to indicate the type [] of Int8. There are other ways to indicate the type of the contents of an array, like providing data when you initialize the array:

Why did the type come up as Int32 when all of those numbers are below 127? Int32 is the default integer type. If you want to force Int8, perhaps for performance reasons, you have to write that explicitly, like so:

(Crystal offers i8, i16, i32, and i64 suffixes for signed integers, and u8, u16, u32, and u64 for unsigned integers.)

You read from the console with gets, which return everything it reads in as a String. You can display the input with string interpolation.

Let’s add the number to our array:

Crystal won’t let you do this:

Add a comment

Related posts:

How to prevent corporate tracking

Over 55 percent of users polled believe you can’t remain anonymous on the Internet. People have become frustrated with reports on corporate tracking that most have become apathetic. Data on anyone…

Extending Classes

You can extend a Java class from a Scala class, and vice versa. For the most part, this should just work. As discussed earlier, if your methods accept function values as parameters, you’ll have…