表达式错误的非法开始:两行错误?

问题描述:

我正在学习作为初学者的学校编码项目。我写的代码很混乱,不太好看,但这是我如何理解它的写法。我只剩下三条线上的18个错误,无法确定我出错的地方。这里是我的错误:表达式错误的非法开始:两行错误?

Band.java:42: illegal start of expression 
    private static void calculate (int numberRows, double[][] positions, int[] rowNumber, double total) { 
    ^

Band.java:42: illegal start of expression 
    private static void calculate (int numberRows, double[][] positions, int[] rowNumber, double total) { 
      ^

Band.java:42: ';' expected 
    private static void calculate (int numberRows, double[][] positions, int[] rowNumber, double total) { 
       ^

Band.java:42: '.class' expected 
    private static void calculate (int numberRows, double[][] positions, int[] rowNumber, double total) { 
            ^

Band.java:42: ';' expected 
    private static void calculate (int numberRows, double[][] positions, int[] rowNumber, double total) { 
               ^

Band.java:42: <identifier> expected 
    private static void calculate (int numberRows, double[][] positions, int[] rowNumber, double total) { 
                     ^

Band.java:42: illegal start of expression 
    private static void calculate (int numberRows, double[][] positions, int[] rowNumber, double total) { 
                      ^

Band.java:42: ';' expected 
    private static void calculate (int numberRows, double[][] positions, int[] rowNumber, double total) { 
                      ^

Band.java:42: not a statement 
    private static void calculate (int numberRows, double[][] positions, int[] rowNumber, double total) { 
                      ^

Band.java:42: ';' expected 
    private static void calculate (int numberRows, double[][] positions, int[] rowNumber, double total) { 
                         ^

Band.java:42: ';' expected 
    private static void calculate (int numberRows, double[][] positions, int[] rowNumber, double total) { 
                            ^

Band.java:85: 'else' without 'if' 
        } else { 
        ^

Band.java:115: illegal start of expression 
    private static double totalWeight(double []singleRow){ 
    ^

Band.java:115: illegal start of expression 
    private static double totalWeight(double []singleRow){ 
      ^

Band.java:115: ';' expected 
    private static double totalWeight(double []singleRow){ 
       ^

Band.java:115: '.class' expected 
    private static double totalWeight(double []singleRow){ 
              ^

Band.java:115: ';' expected 
    private static double totalWeight(double []singleRow){ 
                 ^

Band.java:123: reached end of file while parsing 
} 
^ 
18 errors 

这里是我的代码:

import java.util.Scanner; 
    //=============================================================================                      
    public class Band { 
    //-----------------------------------------------------------------------------                      
     private static Scanner keyboard = new Scanner(System.in); 
    //-----------------------------------------------------------------------------                      
     public static void main(String[] args) { 

      int numberRows; 
      double [][] positions; 
      double total; 
      int numberPositions; 

      System.out.println("Welcome to Band of the Hour"); 
      System.out.println("Please enter number of rows: "); 
      numberRows = keyboard.nextInt(); 
      while (numberRows < 0 && numberRows > 10) { 
       System.out.println("Out of range, try again: "); 
       numberRows = keyboard.nextInt(); 
      } 

     positions = new int[numberRows][]; 

     for (int index=0; index<numberRows; index++) { 
     System.out.println("Please enter number of positions in row " + (char)(rowNumber + (int)'A') + " : "); 
     numberPositions = keyboard.nextInt(); 
     while (numberPositions < 1 && numberPositions > 8) { 
      System.out.println("Out of range, try again: "); 
      } 
     positions = new double[numberRows][numberPositions]; 
     } 
     for(int i=0; i < positions.length; i++) { 
      for(int j=0; j< positions[i].length; j++) { 
       positions[i][j] = 0.0; 
     } 

     calculate(numberRows, positions, rowNumber, total); 

} 

//-----------------------------------------------------------------------------                      
    private static void calculate (int numberRows, double[][] positions, int[] rowNumber, double total) { 
     char operation; 
     int rowLetter; 
     int positionNumber; 
     double weight; 

     do { 
     System.out.println("(A)dd, (R)emove, (P)rint,   e(X)it : "); 
     operation = keyboard.next().toUpper().charAt(0); 
     switch (operation) { 
        case 'A': 
      System.out.println("Please enter row letter :"); 
      rowLetter = (int)keyboard.next().toUpper().charAt(0)-'A'; 
       if (rowLetter < numberRows) { 
           System.out.println("Please enter position number (1 to" + positions[rowNumber].length + ")  :"); 
           positionNumber = keyboard.nextInt(); 
             if (positionNumber >= 1 && positionNumber <= positions[rowNumber].length) { 
             System.out.println("Please enter weight (45.0 to 200.0)"); 
             weight = keyboard.nextDouble(); 
               if (weight >= 45.0 && weight <= 200.0) { 
                 if (total <= 100 * positions[rowNumber].length) { 
                   if (0 = positions[rowNumber][positionNumber]) { 
                   weight = positions[rowNumber][positionNumber]; 
                   System.out.println("****** Musician added."); 
                   } else { 
                   System.out.println("There is already a musician there.  :"); 
                   } 
                 } else { 
                 System.out.println("That would exceed the row weight limit :"); 
                 } 
               } else { 
               System.out.println("ERROR: Out of range, try again  :"); 
               } 
             } else { 
             System.out.println("Out of range, try again"); 
             weight = keyboard.nextDouble(); 
              if (weight >= 45.0 && weight <= 200) { 
              weight = positions[rowNumber][positionNumber]; 
              } else { 
              System.out.println("Out of range, try again"); 
              weight = keyboard.nextDouble();} else { 
           System.out.println("Out of range, try again  :"); 
           positionNumber = keyboard.nextInt(); 
           } 
       } 
         break; 
        case 'R':  
         System.out.println("Please enter row letter :"); 
          rowLetter = (int)keyboard.next().toUpper().charAt(0)-'A'; 
            if (rowLetter < numberRows) { 
            System.out.println("Please enter position number (1 to" + positions[rowNumber].length + ")  :"); 
            positionNumber = keyboard.nextInt(); 
              if (positions[rowNumber][positionNumber] =! 0) { 
              positions[rowNumber][positionNumber] = 0; 
              System.out.println("****** Musician removed."); 
              } else { 
              System.out.println("That position is vacant    :"); 
              } 
            } else { 
            System.out.println("Out of range, try again  :"); 
            } 
          break; 
         case 'P': 
          System.out.printf("%5d ", positions[rowNumber][positionNumber]); 
          break; 
         default: System.out.println("Out of range, try again"); 
         } 
      } while (operation != 'X'); 
     } 
    //-----------------------------------------------------------------------------                      
     private static double totalWeight(double []singleRow){ 
      int index; 
      double total = 0.0; 
      for(index = 0; index < singleRow.length; index++){ 
        total = total + singleRow[index]; 
      } 
      return total; 
    } 
    } 
    //=============================================================================                      
+1

你需要学习如何让你的IDE格式化你的源代码。所有流行的IDE都可以为你做到这一点。然后,更容易看出问题所在,因为大括号会正确缩进。 –

+0

你的代码如此令人难以置信地充满了错误,无法找到从哪里开始。你使用不存在的变量来分配双精度,调用不退出的方法,不闭合花括号,使用int []作为int,尝试赋值给常量等等。我建议你先从小程序,让它免于错误,然后转向更大的东西。 –

你忘了一个右括号。您可能会发现使用IDE可以更轻松地完成这些操作。

+0

我可以在哪里添加右括号?最后? – MC45

+0

我在程序的最后添加了括号,它只修正了我最后的错误 – MC45

第85行的错误是不言而喻的:“其他没有如果”。 85号线是线82的else块起始的右括号与此else块的问题是,它不遵循if语句块:

if (weight >= 45.0 && weight <= 200) { // this line's got your if... 
    weight = positions[rowNumber][positionNumber]; 
} else { // this is fine 
    System.out.println("Out of range, try again"); 
    weight = keyboard.nextDouble(); 
} else { // this is causing the problem 
    System.out.println("Out of range, try again  :"); 
    positionNumber = keyboard.nextInt(); 
} // this line is where the compiler is returning the error 
    // it's the end of the problematic else block 

“达到文件结束,而解析”的错误方式你可能在你的代码末尾丢失了一个右括号(这个东西:})。使用一致的缩进模式通常是一个好主意。这样可以更容易地发现哪些开启和关闭支撑相互匹配,并且更容易查看哪些线路位于哪些环路和语句块的内部或外部。使用IDE也是一个好主意;它可以帮助您不仅适当地格式化,还可以显示哪些大括号与哪些大括号相匹配(例如,如果您单击Netbeans中的一个大括号,它会突出显示大括号及其匹配的一个为黄色;如果它没有匹配一个,它以红色突出显示,因此您可以轻松判断您是否有太多或太少的右花括号)。

我不确定是什么原因导致“非法表达式”错误,但很可能您可能错过了主要方法或之前不同方法中的右大括号。 “非法启动表达式”错误的一个原因是编译器在另一个方法中看到一个方法头。你的方法头文件看起来很好,所以如果在解决“表达式非法启动”错误时它会给你的这些行上的其他错误将得到解决,那么我不会感到惊讶 - 一个错字有时会导致编译器返回几个错误在遇到问题的代码段中。