在输出中 r 表示我有一个错误,即我的条件在我的第一个 if 条件中的长度 > 1
x<-0:100
x
if(x<=39){
"Your grade is E(POOR)"
}else if(x<=49){
"Your grade is D(BELOW AVERAGE)"
}else if(x<=59){
"Your grade is C(AVERAGE)"
}else if(x<=69){
"Your grade is B(ABOVE AVERAGE)"
}else{
"CONGRATULATIONS you scored an A"
}
这里你的
x
是一个向量,而不是 if
条件的单个值
解决方法是迭代
1:100
中的每个值,例如,
Xval <- 0:100
for (x in Xval) {
if (x <= 39) {
print("Your grade is E(POOR)")
} else if (x <= 49) {
print("Your grade is D(BELOW AVERAGE)")
} else if (x <= 59) {
print("Your grade is C(AVERAGE)")
} else if (x <= 69) {
print("Your grade is B(ABOVE AVERAGE)")
} else {
print("CONGRATULATIONS you scored an A")
}
}