我在輸入此代碼時收到無法訪問的代碼錯誤。我不確定爲什麼,是因爲我在這個If語句中沒有Else?接收Java無法訪問的代碼
import java.util.InputMismatchException;
import java.util.Scanner;
public class pickNumbers {
Scanner readInput = new Scanner(System.in);
float[] pickNumbers(int choice) {
float[] myFloats = new float[2];
do { // do loop will continue until user enters correct response
System.out.print("Please enter 2 numbers separated by a space in the formats of floats: "); // will prompt user to enter 2 floats
try {
myFloats[0] = readInput.nextFloat(); // will read first float entered
myFloats[1] = readInput.nextFloat(); // will read second float entered
if (choice == 4 && myFloats[1] == 0.0f)
;
{
System.out.println("Cannot complete calculation. Cannot divide by 0, please try again.");
myFloats[0] = myFloats[1] = 0.0f;
continue;
}
break; // i am receiving an error here saying unreachable code and i don't know why
} catch (final InputMismatchException e) {
System.out.println("You have entered an invalid input. Try again.");
readInput.nextLine(); // discard input that is not a float
continue; // loop will continue until the correct answer is found
}
} while (true);
return myFloats; // an an unreachable error here also
}
}
謝謝你,現在的工作 – userQuestion 2014-11-03 05:33:52