Each iteration of the loop is using too many values, because each call to myReader.next()
is consuming another token. Store the token in a variable, so you can check it multiple times.
while (myReader.hasNext()) {
String token = myReader.next();
if ("Zipline".equals(token)) {
array[i++] = new Zipline(myReader.nextDouble(), myReader.nextDouble());
}
if ("Snorkel".equals(token)) {
array[i++] = new Snorkel(myReader.nextDouble(), myReader.nextDouble());
}
if ("Helicopter".equals(token)) {
array[i++] = new Helicopter(myReader.nextDouble(), myReader.nextDouble());
}
} // while loop
Or use a switch
statement:
while (myReader.hasNext()) {
switch (myReader.next()) {
case "Zipline":
array[i++] = new Zipline(myReader.nextDouble(), myReader.nextDouble());
break;
case "Snorkel":
array[i++] = new Snorkel(myReader.nextDouble(), myReader.nextDouble());
break;
case "Helicopter":
array[i++] = new Helicopter(myReader.nextDouble(), myReader.nextDouble());
break;
}
} // while loop
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…