2014-11-03 71 views
0

我在輸入此代碼時收到無法訪問的代碼錯誤。我不確定爲什麼,是因爲我在這個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 
    } 
} 

回答

2

看着你的if語句,它有一個分號出來的地方,應該是:

if (choice == 4 && myFloats[1] == 0.0f) { 
+0

謝謝你,現在的工作 – userQuestion 2014-11-03 05:33:52

0

;只是因爲這個以下if聲明

if (choice == 4 && myFloats[1] == 0.0f) 

後,您的if聲明已完成,並且以下continue將使break成爲無法訪問的代碼。

1

有一個分號你if (choice == 4 && myFloats[1] == 0.0f)語句後,之後的任何不可達

+0

感謝你的幫助 – userQuestion 2014-11-03 06:36:23