无法弄清楚 while 循环

Cannot figure out while loop

我正在尝试在 R 中创建一个 while 循环,目的是让用户输入一个数字,让代码根据它在向量中的数字位置打印相应的条目,继续提供另一个选择从菜单,然后如果输入 6 则中断循环。我可以让它根据输入的数字提供正确的输出,但它不会通过循环返回。 (我根据下面的评论对代码进行了编辑,但它仍然无法按我需要的方式工作)

menu <- "MENU: \n1. Science Fiction \n2. Computers and Technology \n3. Cooking \n4. Business \n5. Comics \n6. Exit"

menuV <- c("You selected Science Fiction. This is a fun topic.",
           "You selected Computers and Technology. This is a complex topic.",
           "You selected Cooking. This is a passionate topic.",
           "You selected Business. This is a serious topic.",
           "You selected Comics. This is an entertaining topic.")

x <- readline(prompt=paste(menu, "Please select an item from the menu: "))
x <- as.numeric(x)

while (x != 6){
  if(x >= 1 & x <= 7){
    print(menuV[x])
  } else if(x < 1 | x > 7) {
    print("Invalid entry. Please try again.")
  } else {
    print("You exited the menu.")
    break
  }
}

`

下面的方法行得通吗?

menu <- "MENU: \n1. Science Fiction \n2. Computers and Technology \n3. Cooking \n4. Business \n5. Comics \n6. Exit"

menuV <- c("You selected Science Fiction. This is a fun topic.",
           "You selected Computers and Technology. This is a complex topic.",
           "You selected Cooking. This is a passionate topic.",
           "You selected Business. This is a serious topic.",
           "You selected Comics. This is an entertaining topic.")

x <- readline(prompt = paste(menu, "Please select an item from the menu: "))

x <- as.numeric(x)


while (is.numeric(x)) {
  if(x >= 1 & x <= 5){
    print(menuV[x])
    x <- as.numeric(readline(prompt = paste(menu, "Please select an item from the menu: ")))
  } else if(x < 1 | x >= 7) {
    print("Invalid entry. Please try again.")
    x <- as.numeric(readline(prompt = paste(menu, "Please select an item from the menu: ")))
  } else {
    print("You exited the menu.")
    break
  }
}