2015-11-03 174 views
-1

我正在學習作爲初學者的學校編碼項目。我寫的代碼很混亂,不太好看,但這是我如何理解它的寫法。我只剩下三條線上的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,嘗試賦值給常量等等。我建議你先從小程序,讓它免於錯誤,然後轉向更大的東西。 –

回答

1

你忘了一個右括號。您可能會發現使用IDE可以更輕鬆地完成這些操作。

+0

我可以在哪裏添加右括號?最後? – MC45

+0

我在程序的最後添加了括號,它只修正了我最後的錯誤 – MC45

0

第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中的一個大括號,它會突出顯示大括號及其匹配的一個爲黃色;如果它沒有匹配一個,它以紅色突出顯示,因此您可以輕鬆判斷您是否有太多或太少的右花括號)。

我不確定是什麼原因導致「非法表達式」錯誤,但很可能您可能錯過了主要方法或之前不同方法中的右大括號。 「非法啓動表達式」錯誤的一個原因是編譯器在另一個方法中看到一個方法頭。你的方法頭文件看起來很好,所以如果在解決「表達式非法啓動」錯誤時它會給你的這些行上的其他錯誤將得到解決,那麼我不會感到驚訝 - 一個錯字有時會導致編譯器返回幾個錯誤在遇到問題的代碼段中。

相關問題