Bắt đầu ngayBắt đầu miễn phí

Check input data type

Conditional statements make your code intelligent so that it can respond to your inputs or to the results it calculates.

One common use for conditional statements is to respond to the users' inputs. In this example, let's say you have written some code for an online shop that will return a number n of this week's top-rated products. This code only works if the input n is an integer (a whole number). You want to check if n is suitable and print a message to help the user.

The input n has already been assigned and is available in your environment.

Bài tập này là một phần của khóa học

Introduction to Julia

Xem khóa học

Hướng dẫn bài tập

  • Write an if statement to check whether the type of parameter n is equal to Int64.
  • If it is this type, print the statement "n is the correct type".
  • Write an else block so that the message "This script only works on integers" is printed otherwise.
  • End the conditional statement.

Bài tập tương tác thực hành trực tiếp

Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.

# Check if n is the correct type
if ____==____
	# Print n is the correct type
	println(____)
# Use an else statement to print if n is not the correct type
____
	____(____)
# Finish the conditional statement
____

println(n)
Chỉnh sửa và Chạy Mã