没有If的其他(需要帮助)

时间:2013-09-09 01:06:14

标签: java if-statement brackets

我必须在这里遗漏一些东西,但我无法看到这段代码中的错误在哪里 Test.java:63:错误:非法启动表达式

                            }else 
                            ^

Test.java:63:错误:'else'没有'if'

                           }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;                                         
                }
        }

3 个答案:

答案 0 :(得分:2)

在其他地方打开括号?

if(){

} else {

)

答案 1 :(得分:1)

我认为您对if/else构造的理解存在问题。您可能需要也可能不需要else方案,因此在这种情况下无需使用else。正如您在代码中放置了多个这样的else,这似乎毫无用处。您似乎只是使用它,就好像它被强制要求elseif一起使用。也总是使用花括号来开始和结束if if else else以避免错误。

以下标记似乎没必要:

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

答案 2 :(得分:0)

如何使用if-else statement

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

您在返回return award;时也犯了错误。你放在循环中。在循环结束后放置

这是格式化代码试试

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
        }