-4

我一定是在这里遗漏了一些东西,但我看不到这段代码中的错误在哪里一直说 Test.java:63: 错误:表达式的非法开始

                            }else 
                            ^

Test.java:63:错误:没有“if”的“else”

                           }else



public static int[] awardcal(int awardsum, int[] award, double[] gpa, boolean[] need, int count, double classadvarge){
                for(int index = 0; index < count; index ++){   
                      if (gpa[index] == 4.00){
                            awardsum += 1000;
                                if(need[index] == true){
                            awardsum += 500;
                                }else awardsum += 200;    
                      }else 
                      if (gpa[index] <= 3.70 && gpa[index] < 4.00){
                            if (need[index] == true){
                                   awardsum += 500;
                                if (gpa[index] >= classaverage){
                                   awardsum += 500;
                                }else     
                           }else
                      }else                              
                      if (gpa[index] >= classaverage){
                         awardsum += 200;
                      }else

                      if (gpa[index] >= classaverage){
                            if (need[index] == true){
                                awardsum += 500;
                            }else 
                                awardsum += 200;
                      }else 
                 award[index] = awardsum;
                 return award;                                         
                }
        }
4

3 回答 3

2

else之后开括号?

if(){

} else {

)
于 2013-09-09T01:11:24.560 回答
1

if/else我觉得你对construct的理解有问题。您可能需要也可能不需要else场景,因此无需else在这种情况下使用。正如您else在代码中放置了多个这样的,这似乎没有用。似乎您只是使用它,就好像它被强制使用elsewith一样if。也总是使用花括号来开始和结束 if 或 else 块以避免错误。

以下标记的 else 似乎没有必要:

     if (gpa[index] >= classaverage){
         awardsum += 500;
      }else     // seems not required
 }else // seems not required
于 2013-09-09T01:11:39.167 回答
0

如何使用if-else 语句

   if (gpa[index] >= classaverage)
        {
             awardsum += 500;
        }
         else //unwanted else    
     }else //unwanted else
    }else                              
    if (gpa[index] >= classaverage){

您在退货时也犯了错误return award;。您将 with 放在循环中。循环结束后放置

这是格式化的代码试试

public static int[] awardcal(int awardsum, int[] award, double[] gpa, boolean[] need, int count, double classadvarge)
    {
        for (int index = 0; index < count; index++)
        {
            if (gpa[index] == 4.00)
            {
                awardsum += 1000;
                if (need[index] == true)
                {
                    awardsum += 500;
                }
                else
                {
                    awardsum += 200;
                }
            }
            else if (gpa[index] <= 3.70 & gpa[index] < 4.00)
            {
                if (need[index] == true)
                {
                    awardsum += 500;
                    if (gpa[index] >= classaverage)
                    {
                        awardsum += 500;
                    }
                    else //unused else
                    {

                    }
                }
                else //unused else
                {

                }
            }
            else if (gpa[index] >= classaverage)
            {
                awardsum += 200;
            }
            else if (gpa[index] >= classaverage)
            {
                if (need[index] == true)
                {
                    awardsum += 500;
                }
                else
                {
                    awardsum += 200;
                }
            }
            else
            {


        award[index] = awardsum;
                }
//            return award; // you wrongly placed the return statement
            }
            return award;  // This will return the whole array
        }
于 2013-09-09T01:12:29.523 回答