我正在作为初学者从事学校编码项目。我写的代码很乱,而且样式不好,但这就是我理解如何编写的代码。我只剩下三条线上的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;
}
}
//=============================================================================
答案 0 :(得分:1)
你忘记了一个亲密的人。您可能会发现使用IDE更容易使这些事情变得方便。
答案 1 :(得分: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中的一个大括号,它会突出显示该大括号及其匹配的大括号;如果它没有匹配一,它用红色突出显示它,这样你就可以很容易地判断你是否有一个太多或一个太少的关闭括号。
我不确定是什么导致了“非法开始表达”错误,但很可能你可能错过了主方法或不同的先前方法中的右括号。 “非法启动表达式”错误的一个原因是编译器在另一个方法中看到方法头。您的方法标题看起来很好,所以如果解决了“非法启动表达式”错误后,它会解决这些行中的其他错误,我不会感到惊讶 - 单个拼写错误有时会导致编译器返回多个错误在代码中遇到问题的部分。