Я пытаюсь создать код, который сначала проверяет, существует ли файл (если он существует, используйте его, если нет, создайте новый). Затем спросите пользователя два имени (name1 и name2), и это повторяется, спросите игрока, хочет ли он изменить имя, с некоторыми другими ограничениями. Проблема в том, что когда пользователь вводит первое несуществующее имя (name1 не в "gamers"), когда код запрашивает второе имя (name2), я не знаю, почему код сохраняет name2, каким бы оно ни было (код не доказывает, что он есть у «геймеров»). Но если мы введем name1 в «gamers», name2 работает, но если name1 не повторяется, а код запрашивает name2, код сохраняет name2 как бы то ни было, без подтверждения, что имя в «gamers» , Вот мой код:
checknames <- function(){
if(file.exists("Players.txt")){
table <- read.table(file = "Players.txt", header = TRUE)
gamers <- table[,1]
games <- table[,2]
scores <- table[,3]
table <- data.frame(gamers,games,scores)
}
else{
gamers <- c()
games <- c()
scores <- c()
table <- data.frame(gamers,games,scores)
}
r=1
p=1
repeat{
if(r==1){
print("Name Player 1: ")
name1=scan(,what="character",1)
if(length(name1)>0){
if(any(name1==gamers)){
r=readline(prompt = "This player is already in the file. Would you like to change the name? \n 1. Yes \n 2. No \n Select an option: ")
if(r==0){
r<-99
}
}
else{
r <- 0
}
}
else{
print("This name is invalid. Please insert again Player Name 1")
}
}
else if(r==2){
break
}
else if(r==0){
gamers=c(gamers,name1)
name1 <- data.frame(gamers=name1,games="1",scores="100")
table <- rbind(table,name1)
write.table(table,file="Players.txt")
break
}
else{
repeat{
r=readline(prompt = "This option is unavailable, please choose another one: \n 1. Yes \n 2. No \n Select an option: ")
if(r==1|r==2){
break
}
}
}
table
}
repeat{
if(p==1){
table <- read.table(file = "Players.txt", header = TRUE)
print("Name Player 2: ")
name2=scan(,what="character",1)
if(length(name2)>0){
if(any(name2==gamers)){
p=readline(prompt = "This player is already in the file. Would you like to change the name? \n 1. Yes \n 2. No \n Select an option: ")
if(p==0){
p<-99
}
}
else{
p <- 0
}
}
else{
print("This name is invalid. Please insert again Player Name 2")
}
}
else if(p==2){
break
}
else if(p==0){
gamers=c(gamers,name2)
name2 <- data.frame(gamers=name2,games="1",scores="100")
table <- rbind(table,name2)
write.table(table,file="Players.txt")
break
}
else{
repeat{
p=readline(prompt = "This option is unavailable, please chose another one: \n 1. Yes \n 2. No \n Select an option: ")
if(p==1|p==2){
break
}
}
}
}
table
}
table <-checknames()
Надеюсь, вы можете мне помочь, спасибо!