2
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Tôi muốn nhắc người dùng cho một đầu vào và nếu đó không phải là tùy chọn hợp lệ cho đến khi nhập hợp lệ.
Ngay bây giờ, nó luôn luôn tái cấu trúc không quan trọng đầu vào, đúng hay không.
Đây là mã:
var userChoice = prompt["Do you choose rock, paper, scissors or rope?"];
while [userChoice != "rock" || "paper" || "scissors" || "rope"] {
userChoice = prompt["Sorry invalid input, please enter either: rock, paper,scissors, or rope."];
}
Có vẻ như đó là một điều đơn giản để làm, tôi có hiểu lầm cách sử dụng trong khi các vòng không? Có lẽ các nhà khai thác đã sai? Không thể tìm thấy bất cứ điều gì hữu ích, bất kỳ trợ giúp nào được đánh giá cao!
j08691
200K31 Huy hiệu vàng253 Huy hiệu bạc267 Huy hiệu Đồng31 gold badges253 silver badges267 bronze badges
Đã hỏi ngày 4 tháng 1 năm 2016 lúc 21:31Jan 4, 2016 at 21:31
1
Bạn cũng có thể sử dụng một mảng và indexof
var userChoice = prompt["rock, paper, scissors, rope?"].toLowerCase[];
while [["rock", "paper", "scissors", "rope"].indexOf[userChoice] === -1] {
userChoice = prompt["Please choose: rock, paper, scissors, rope?"].toLowerCase[];
}
alert["You chose '" + userChoice + "'"];
Đã trả lời ngày 4 tháng 1 năm 2016 lúc 21:39Jan 4, 2016 at 21:39
SMCDSMCDsmcd
3.0772 Huy hiệu vàng15 Huy hiệu bạc27 Huy hiệu đồng2 gold badges15 silver badges27 bronze badges
3
Cú pháp
var userChoice = prompt["rock, paper, scissors, rope?"].toLowerCase[];
while [["rock", "paper", "scissors", "rope"].indexOf[userChoice] === -1] {
userChoice = prompt["Please choose: rock, paper, scissors, rope?"].toLowerCase[];
}
alert["You chose '" + userChoice + "'"];
1 là hợp lệ, nhưng hoặc sẽ liên kết muộn hơn so với kiểm tra bình đẳng, vì vậy về cơ bản bạn viết:while [[userChoice != "rock"] || "paper" || "scissors" || "rope"] {
//...
}
Bây giờ vì bạn sử dụng như vậy, về cơ bản bạn thực hiện kiểm tra hiệu quả và ít nhất
var userChoice = prompt["rock, paper, scissors, rope?"].toLowerCase[];
while [["rock", "paper", "scissors", "rope"].indexOf[userChoice] === -1] {
userChoice = prompt["Please choose: rock, paper, scissors, rope?"].toLowerCase[];
}
alert["You chose '" + userChoice + "'"];
2 có hiệu quả. Tuy nhiên, bạn có thể có được hành vi bạn nhắm, bằng cách sử dụng kiểm tra rõ ràng và sử dụng toán tử var userChoice = prompt["rock, paper, scissors, rope?"].toLowerCase[];
while [["rock", "paper", "scissors", "rope"].indexOf[userChoice] === -1] {
userChoice = prompt["Please choose: rock, paper, scissors, rope?"].toLowerCase[];
}
alert["You chose '" + userChoice + "'"];
3 thay vì var userChoice = prompt["rock, paper, scissors, rope?"].toLowerCase[];
while [["rock", "paper", "scissors", "rope"].indexOf[userChoice] === -1] {
userChoice = prompt["Please choose: rock, paper, scissors, rope?"].toLowerCase[];
}
alert["You chose '" + userChoice + "'"];
4:while [userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors" && userChoice !== "rope"] {
//...
}
Đã trả lời ngày 4 tháng 1 năm 2016 lúc 21:39Jan 4, 2016 at 21:39
SMCDSMCDWillem Van Onsem
3.0772 Huy hiệu vàng15 Huy hiệu bạc27 Huy hiệu đồng29 gold badges390 silver badges512 bronze badges
Cú pháp
var userChoice = prompt["rock, paper, scissors, rope?"].toLowerCase[];
while [["rock", "paper", "scissors", "rope"].indexOf[userChoice] === -1] {
userChoice = prompt["Please choose: rock, paper, scissors, rope?"].toLowerCase[];
}
alert["You chose '" + userChoice + "'"];
1 là hợp lệ, nhưng hoặc sẽ liên kết muộn hơn so với kiểm tra bình đẳng, vì vậy về cơ bản bạn viết:while [userChoice != "rock" && userChoice != "paper" && userChoice != "scissors" && userChoice != "rope"] {
Bây giờ vì bạn sử dụng như vậy, về cơ bản bạn thực hiện kiểm tra hiệu quả và ít nhất
var userChoice = prompt["rock, paper, scissors, rope?"].toLowerCase[];
while [["rock", "paper", "scissors", "rope"].indexOf[userChoice] === -1] {
userChoice = prompt["Please choose: rock, paper, scissors, rope?"].toLowerCase[];
}
alert["You chose '" + userChoice + "'"];
2 có hiệu quả. Tuy nhiên, bạn có thể có được hành vi bạn nhắm, bằng cách sử dụng kiểm tra rõ ràng và sử dụng toán tử var userChoice = prompt["rock, paper, scissors, rope?"].toLowerCase[];
while [["rock", "paper", "scissors", "rope"].indexOf[userChoice] === -1] {
userChoice = prompt["Please choose: rock, paper, scissors, rope?"].toLowerCase[];
}
alert["You chose '" + userChoice + "'"];
3 thay vì var userChoice = prompt["rock, paper, scissors, rope?"].toLowerCase[];
while [["rock", "paper", "scissors", "rope"].indexOf[userChoice] === -1] {
userChoice = prompt["Please choose: rock, paper, scissors, rope?"].toLowerCase[];
}
alert["You chose '" + userChoice + "'"];
4:Jan 4, 2016 at 21:36
Willem Van Onsemwillem Van OnsemHogan
412K29 Huy hiệu vàng390 Huy hiệu bạc512 Huy hiệu Đồng10 gold badges76 silver badges115 bronze badges
Trong khi Userchoice không bằng bất kỳ lựa chọn nào ... vì vậy và
var userChoice;
while [!~["rock", "paper", "scissors", "rope"].indexOf[userChoice]] {
userChoice = prompt["Sorry invalid input, please enter either: rock, paper,scissors, or rope."];
}
Đã trả lời ngày 4 tháng 1 năm 2016 lúc 21:39Jan 4, 2016 at 21:39
SMCDSMCDNina Scholz
3.0772 Huy hiệu vàng15 Huy hiệu bạc27 Huy hiệu đồng24 gold badges326 silver badges366 bronze badges
Cú pháp
var userChoice = prompt["rock, paper, scissors, rope?"].toLowerCase[];
while [["rock", "paper", "scissors", "rope"].indexOf[userChoice] === -1] {
userChoice = prompt["Please choose: rock, paper, scissors, rope?"].toLowerCase[];
}
alert["You chose '" + userChoice + "'"];
1 là hợp lệ, nhưng hoặc sẽ liên kết muộn hơn so với kiểm tra bình đẳng, vì vậy về cơ bản bạn viết:ignoring case sensitivity], it is possible to use regular expressions, e.g.
match:while[ ! userChoice.match[/^\s*[?:rock|paper|scissors|rope]\s*$/i]] { ... }
Bây giờ vì bạn sử dụng như vậy, về cơ bản bạn thực hiện kiểm tra hiệu quả và ít nhất
var userChoice = prompt["rock, paper, scissors, rope?"].toLowerCase[];
while [["rock", "paper", "scissors", "rope"].indexOf[userChoice] === -1] {
userChoice = prompt["Please choose: rock, paper, scissors, rope?"].toLowerCase[];
}
alert["You chose '" + userChoice + "'"];
2 có hiệu quả. Tuy nhiên, bạn có thể có được hành vi bạn nhắm, bằng cách sử dụng kiểm tra rõ ràng và sử dụng toán tử var userChoice = prompt["rock, paper, scissors, rope?"].toLowerCase[];
while [["rock", "paper", "scissors", "rope"].indexOf[userChoice] === -1] {
userChoice = prompt["Please choose: rock, paper, scissors, rope?"].toLowerCase[];
}
alert["You chose '" + userChoice + "'"];
3 thay vì var userChoice = prompt["rock, paper, scissors, rope?"].toLowerCase[];
while [["rock", "paper", "scissors", "rope"].indexOf[userChoice] === -1] {
userChoice = prompt["Please choose: rock, paper, scissors, rope?"].toLowerCase[];
}
alert["You chose '" + userChoice + "'"];
4:Willem Van Onsemwillem Van Onsem
412K29 Huy hiệu vàng390 Huy hiệu bạc512 Huy hiệu Đồng
var validChoices = {rock: true,
paper: true,
scissors: true,
rope: true},
userChoice = ...
while [ ! validChoices.hasOwnProperty[userChoice]] {
...
}
Trong khi Userchoice không bằng bất kỳ lựa chọn nào ... vì vậy và
Đã trả lời ngày 4 tháng 1 năm 2016 lúc 21:36Jan 4, 2016 at 21:45
HoganhoganAprillion
67.6K10 Huy hiệu vàng76 Huy hiệu bạc115 Huy hiệu đồng4 gold badges54 silver badges87 bronze badges
2
Có lẽ một mảng thông minh hơn để sử dụng với
var userChoice = prompt["rock, paper, scissors, rope?"].toLowerCase[];
while [["rock", "paper", "scissors", "rope"].indexOf[userChoice] === -1] {
userChoice = prompt["Please choose: rock, paper, scissors, rope?"].toLowerCase[];
}
alert["You chose '" + userChoice + "'"];
5.Nina Scholznina Scholz
361K24 Huy hiệu vàng326 Huy hiệu bạc366 Huy hiệu Đồngat least one of the operands is
var userChoice = prompt["rock, paper, scissors, rope?"].toLowerCase[];
while [["rock", "paper", "scissors", "rope"].indexOf[userChoice] === -1] {
userChoice = prompt["Please choose: rock, paper, scissors, rope?"].toLowerCase[];
}
alert["You chose '" + userChoice + "'"];
8. Đối với các quy tắc phức tạp hơn [chẳng hạn như bỏ qua độ nhạy của trường hợp], có thể sử dụng các biểu thức thông thường, ví dụ: cuộc thi đấu:
userChoice = "rock";
userChoice != "rock" => FALSE
.
"rock" != "paper" => TRUE
Lưu ý: Cảm ơn Willem Van Onsem vì đã đề xuất Regex
Cũng có khả năng là những người theo nghĩa chữ:
hoặc ES6 đặt cho các trình duyệt mới nhất kể từ năm 2016:
var userChoice = prompt["rock, paper, scissors, rope?"].toLowerCase[];
while [["rock", "paper", "scissors", "rope"].indexOf[userChoice] === -1] {
userChoice = prompt["Please choose: rock, paper, scissors, rope?"].toLowerCase[];
}
alert["You chose '" + userChoice + "'"];
6at least one of the operands evaluates to while [[userChoice != "rock"] || "paper" || "scissors" || "rope"] {
//...
}
5. Đã trả lời ngày 4 tháng 1 năm 2016 lúc 21:45
userChoice = "rock";
userChoice != "rock" => FALSE
Aprillionaprillion
20.4K4 Huy hiệu vàng54 Huy hiệu bạc87 Huy hiệu đồngFeb 21, 2019 at 8:37
Tôi sẽ thử một câu trả lời chung hơn:ruslaniv
Vòng lặp
7 chạy trong khi bất cứ điều gì trong ngoặc là var userChoice = prompt["rock, paper, scissors, rope?"].toLowerCase[];
while [["rock", "paper", "scissors", "rope"].indexOf[userChoice] === -1] {
userChoice = prompt["Please choose: rock, paper, scissors, rope?"].toLowerCase[];
}
alert["You chose '" + userChoice + "'"];
8.1 silver badge12 bronze badgesvar userChoice = prompt["rock, paper, scissors, rope?"].toLowerCase[];
while [["rock", "paper", "scissors", "rope"].indexOf[userChoice] === -1] {
userChoice = prompt["Please choose: rock, paper, scissors, rope?"].toLowerCase[];
}
alert["You chose '" + userChoice + "'"];