#Implement a multiplication game. A while loop that gives the user two random numbers from 2 to 12 and asks the user to #multiply them. Only exit the loop after five correct answers.
total <- 0
while (TRUE) {
if (!total %in% 0:4) {
total <- 0L
}
n <- sample(c(2:12), 2)
cat("What is the product of ", n[1], " and ", n[2], "?\n", sep = "")
x <- as.integer(readline())
if (x == prod(n)) {
total <- total + 1L
cat("Right!")
if (total == 5) break
cat(" You just need", 5 - total, "more answers.\n\n")
} else {
cat("Wrong\n")
}
}
# if (!total %in% 0:4) {
total <- 0L
}#
不知道划线部分有何作用?去掉对整个程序运行并没有影响。