checknames <- function(){
gamers <- c("Rebeca","Luis","Paco")
games <- c(3,2,7)
scores <- c(100,110,50)
table <- data.frame(gamers,games,scores)
r=0
p=0
repeat{
print("Name Player 1: ")
name1=scan(,what="character",1)
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: ")
}
else{
r <- 0
}
if(r==2){
break
}
if(r==0){
gamers=c(gamers,name1)
name1 <- data.frame(gamers=name1,games="1",scores="100")
table <- rbind(table,name1)
break
}
}
table
repeat{
print("Name Player 2: ")
name2=scan(,what="character",1)
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: ")
}
else{
p <- 0
}
if(p==2){
break
}
if(p==0){
gamers=c(gamers,name2)
name2 <- data.frame(gamers=name2,games="1",scores="100")
table <- rbind(table,name2)
break
}
}
table
}
table <-checknames()
大家好,我做了一个代码,要求用户输入2个名称,如果此名称在表中,它应该询问用户是否要更改其名称。问题是,当他是否要更改姓名的问题时,我给他两个选项:“ 1”代表“是”,“ 2”代表“否”,当用户输入其他字符(例如“ t”或“代码为“ 7”(例如),代码显示:“此选项不可用,请选择另一个”或类似的名称,然后再次询问他是否要更改姓名。希望您能帮助我,非常感谢!
您可以将p
checkif
语句重组为ifelse
,以按顺序检查所需的案例,如果p=2
然后,如果p=0
最后p not= 1
p=1
repeat{
if(p==1){
print("Name Player 2: ")
name2=scan(,what="character",1)
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 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)
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}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句