-
Notifications
You must be signed in to change notification settings - Fork 0
Input & Output
Unlike Python and C, you need to call the input function from the io module to a variable in a similar fashion to Java's Scanner class.
Once you have a variable you called the function to, you can start using it to read user input. To do that, you would do it similarly to Java by using the read attributes of the reader class that powers input:
Types of reads:
-
readLine: Returnsarray<char> -
readInt: Returnsint -
readDec: Returnsdec -
readChar: Returnschar -
readAll: Takes in anarray<char>and returns the first entry of thatarray<char>.
In this small example below, the input function is being called to the variable input and is being used to take in user input.
include "io.nhp"
def main():
input = io::input()
favFood = input.readLine()
Like input, you need to call the output function to a variable to be able to have the ability to create output in the console.
Once you have done this, you can create output by using the writeLine attribute of output. Like in C and Python, you can print the value of variables using this.
The example below uses both input and output to take user input and print it.
include "io.nhp"
def main():
input = io::input()
out = io::output()
out.writeLine("What is your favorite highway?")
highway = input.readLine()
out.writeLine("Your favorite highway is:")
out.writeLine(highway)