import 'dart:io';
void main() {
print('Input integers (q to stop):');
final integers = <int>[];
while (true) {
// Reads input from the user.
final input = stdin.readLineSync()!;
// Check to see if the user is done inputting numbers.
if (input == 'q') {
break;
}
// Try to convert the String to an int. If input isn't a
// valid integer, int.tryParse(input) == null.
final integer = int.tryParse(input);
if (integer != null) {
integers.add(integer);
}
}
print('Original order: $integers');
// Reversing a List in Dart is simple: just call integers.reverse
// to get an Iterable with the elements of integers in reversed order.
// Calling integers.reverse.toList() will convert the Iterable to a List
// so it's possible to print the entire list at once.
print('Reversed order: ${integers.reversed.toList()}');
}
示例:
Input integers (q to stop):
1
2
3
r
4
5
q
Original order: [1, 2, 3, 4, 5]
Reversed order: [5, 4, 3, 2, 1]
1条答案
按热度按时间2fjabf4q1#
您可以执行以下操作:
示例: