2014-11-03 75 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