我正在學習作爲初學者的學校編碼項目。我寫的代碼很混亂,不太好看,但這是我如何理解它的寫法。我只剩下三條線上的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;
}
}
//=============================================================================
你需要學習如何讓你的IDE格式化你的源代碼。所有流行的IDE都可以爲你做到這一點。然後,更容易看出問題所在,因爲大括號會正確縮進。 –
你的代碼如此令人難以置信地充滿了錯誤,無法找到從哪裏開始。你使用不存在的變量來分配雙精度,調用不退出的方法,不閉合花括號,使用int []作爲int,嘗試賦值給常量等等。我建議你先從小程序,讓它免於錯誤,然後轉向更大的東西。 –