Dart - стандартный ввод-вывод
Стандартный ввод в Dart:
In Dart programming language, you can take standard input from the user through the console via the use of .readLineSync()
function. To take input from the console you need to import a library, named dart:io
from libraries of Dart.
About Stdin Class:
This class allows the user to read data from standard input in both synchronous and asynchronous ways. The method readLineSync()
is one of the method use to take input from user. Refer to offical doc for other method, from here.
Taking a string input from user –
// importing dart:io file import "dart:io" ; void main() { print( "Enter your name?" ); // Reading name of the Geek String name = stdin.readLineSync(); // Printing the name print( "Hello, $name!
Welcome to GeeksforGeeks!!" ); } |
Вход:
Компьютерщик
Выход:
Enter your name? Hello, Geek! Welcome to GeeksforGeeks!!
Taking integer value as input –
// Importing dart:io file import "dart:io" ; void main() { // Asking for favourite number print( "Enter your favourite number:" ); // Scanning number int n = int .parse(stdin.readLineSync()); // Printing that number print( "Your favourite number is $n" ); } |
Вход:
01
Выход:
Введите свой любимый номер: Ваше любимое число - 1
Стандартный вывод в Dart:
В dart есть два способа отобразить вывод в консоли:
- Использование оператора печати.
- Использование оператора stdout.write ().
Printing Output in two different ways –
import "dart:io" ; void main() { // Printing in first way print( "Welcome to GeeksforGeeks! // printing from print statement" ); // Printing in second way stdout.write( "Welcome to GeeksforGeeks! // printing from stdout.write()" ); } |
Выход:
Добро пожаловать в GeeksforGeeks! // печать из оператора печати Добро пожаловать в GeeksforGeeks! // печать из stdout.write ()
Примечание:
Оператор print () переводит курсор на следующую строку, в то время как stdout.write () не переводит курсор на следующую строку, он остается в той же строке.
If the print
statements are switched in the above program then:
Output:
Welcome to GeeksforGeeks! // printing from stdout.write()Welcome to GeeksforGeeks! // printing from print statement
Making a simple addition program –
import "dart:io" ; void main() { print( "-----------GeeksForGeeks-----------" ); print( "Enter first number" ); int n1 = int .parse(stdin.readLineSync()); print( "Enter second number" ); int n2 = int .parse(stdin.readLineSync()); // Adding them and printing them int sum = n1 + n2; print( "Sum is $sum" ); } |
Вход:
11 12
Выход:
----------- GeeksForGeeks ----------- Введите первое число Введите второй номер Сумма 23