我正在尝试创建一个程序,它会一直询问您循环中的第一个,中间名和姓,直到您输入'EXIT'
或'exit'
作为姓氏。我在这里使用的||
语句运算符不起作用,我似乎无法找到答案。有什么想法吗?
var FirstName = '' ;
var MiddleName = '' ;
var LastName = '' ;
var FullName = '' ;
while (LastName != 'EXIT' || LastName != 'exit' ) {
FirstName = prompt ('What is your first name');
MiddleName = prompt ('What is your middle name');
LastName = prompt ('What is your last name');
FullName = FirstName + ' ' + MiddleName + ' ' + LastName;
alert ('Welcome, ' + FullName);
}
干杯:)
答案 0 :(得分:2)
你需要检查你的布尔逻辑。这是一个有效的代码示例:
var FirstName = '' ;
var MiddleName = '' ;
var LastName = '' ;
var FullName = '' ;
while (LastName != 'EXIT' && LastName != 'exit' ) { /* updated */
FirstName = prompt ('What is your first name');
MiddleName = prompt ('What is your middle name');
LastName = prompt ('What is your last name');
FullName = FirstName + ' ' + MiddleName + ' ' + LastName;
alert ('Welcome, ' + FullName);
}
!A AND !B
而不是!A OR !B
- 后者在您的情况下始终为真,因为您将同一个变量与两个不同的字符串进行比较。