Dart:我可以在满足条件后结束递归吗
Dart: can i make a recursion end after meeting a condition
我写了一个收集姓名的函数,如果用户省略了输入,函数returns“你跳过了..”并再次请求它,但是即使我输入了一个输入,这个函数仍然在递归,它仍然一遍又一遍地问,就像我把它留空一样
void main() {
print('what is your name bro:');
String? name = stdin.readLineSync();
void ncon() {
if (name == '') {
print('Name can\'t be empty \n\nWhat is your name bro:');
String? name = stdin.readLineSync();
ncon();
}
}
ncon();
print('How are you $name');
}
如果用户最终输入名称并直接跳到 print('How are you $name');
,我希望递归结束
不要使这个递归。使用 while
循环!
void main() {
print('what is your name bro:');
var name = stdin.readLineSync()!;
while (name.isEmpty) {
print('Name can\'t be empty \n\nWhat is your name bro:');
name = stdin.readLineSync()!;
}
print('How are you $name');
}
我写了一个收集姓名的函数,如果用户省略了输入,函数returns“你跳过了..”并再次请求它,但是即使我输入了一个输入,这个函数仍然在递归,它仍然一遍又一遍地问,就像我把它留空一样
void main() {
print('what is your name bro:');
String? name = stdin.readLineSync();
void ncon() {
if (name == '') {
print('Name can\'t be empty \n\nWhat is your name bro:');
String? name = stdin.readLineSync();
ncon();
}
}
ncon();
print('How are you $name');
}
如果用户最终输入名称并直接跳到 print('How are you $name');
不要使这个递归。使用 while
循环!
void main() {
print('what is your name bro:');
var name = stdin.readLineSync()!;
while (name.isEmpty) {
print('Name can\'t be empty \n\nWhat is your name bro:');
name = stdin.readLineSync()!;
}
print('How are you $name');
}