Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中的If-Else语句-Else中意外的Else_R_Loops_If Statement_Indentation - Fatal编程技术网

R中的If-Else语句-Else中意外的Else

R中的If-Else语句-Else中意外的Else,r,loops,if-statement,indentation,R,Loops,If Statement,Indentation,我有一个赋值,只使用if,else语句对向量中的3个数字进行R排序。我已经对括号做了很多更改,但仍然在控制台中出现“意外的else in else”错误 x <- c(200, 700, 1000) if ((x[1] > x[2]) & (x[1] > x[3])) { if (x[2] > x[3]) { ord <- c(x[1], x[2], x[3]) } else { ord <- c(x[1], x[3], x[

我有一个赋值,只使用if,else语句对向量中的3个数字进行R排序。我已经对括号做了很多更改,但仍然在控制台中出现“意外的else in else”错误

x <- c(200, 700, 1000)

if ((x[1] > x[2]) & (x[1] > x[3])) {
  if (x[2] > x[3]) {
    ord <- c(x[1], x[2], x[3])
  } else {
    ord <- c(x[1], x[3], x[2])
  }
}
else if ((x[2] > x[1]) & (x[2] > x[3]) {
  if (x[1] > x[3]) {
    ord <- c(x[2], x[1], x[3])
  } else {
    ord <- c(x[2], x[3], x[1])
  }
}
else if {
  (x[1] > x[2])
  ord <- c(x[3], x[1], x[2])
} else {
  ord <- c(x[3], x[2], x[2])
}
print(ord)
x[2])&(x[1]>x[3])){
如果(x[2]>x[3]){
ord x[3]){
如果(x[1]>x[3]){

ord我已经能够通过将语句中的“else-if”部分移到else前面来纠正这一点,这是有效的。不幸的是,关于这个范例还不清楚

if((x[1]>x[2]) & (x[1]>x[3])){
   if(x[2]>x[3]){
      ord <- c(x[1],x[2],x[3])}
    else {ord <- c(x[1],x[3],x[2])}} else if((x[2]>x[1]) & (x[2]>x[3])){
    if(x[1]>x[3]){
          ord <- c(x[2],x[1],x[3])}
    else  {ord <- c(x[2],x[3],x[1])}} else if (x[1]>x[2]){
     ord <- c(x[3],x[1],x[2])} else {ord <- c(x[3],x[2],x[1])}
if((x[1]>x[2])&(x[1]>x[3])){
如果(x[2]>x[3]){
ord x[3])){
如果(x[1]>x[3]){

ord错误在从末尾开始的第8行

x <- c(200, 700, 1000)

if ((x[1] > x[2]) & (x[1] > x[3])) {
  if (x[2] > x[3]) {
    ord <- c(x[1], x[2], x[3])
  }
  else {
    ord <- c(x[1], x[3], x[2])
  }
}
else if ((x[2] > x[1]) & (x[2] > x[3]) {
  if (x[1] > x[3]) {
    ord <- c(x[2], x[1], x[3])
  }
  else  {
    ord <- c(x[2], x[3], x[1])
  }
}
else if(x[1] > x[2]){ # I moved the following line here
  # (x[1] > x[2]) # this is a mistake
  ord <- c(x[3], x[1], x[2])
}
else {
  ord <- c(x[3], x[2], x[2])
}

print(ord)
x[2])&(x[1]>x[3])){
如果(x[2]>x[3]){
ord x[3]){
如果(x[1]>x[3]){
ord x[2])#这是一个错误

你的两个花括号是错误的。第4行中的一个属于第3行。下面的另一个if语句也是一样。嗨!你一定要遵循一些风格指南,比如。这真的很有帮助,特别是当你刚刚开始编程的时候。我已经把你的代码和它对齐了,也许你会发现它更容易理解现在调试代码?