How to break this while(true) loop?

后端 未结 2 1495
温柔的废话
温柔的废话 2021-01-27 03:00

Hi I am trying to break from this loop and return the coordinates when both if statements are true. However the loop is never ending. How can I fix it?

public          


        
相关标签:
2条回答
  • 2021-01-27 03:17

    See if this works. I just made a Boolean flag variable to get out the while loop. It should make the flag false once it reaches the second if.

    public static String[] positionQuery(int dim, Scanner test_in) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Provide origin and destination coordinates.");
            System.out.println("Enter two positions between A1-H8:");
            Boolean flag = true;
            while(flag) {
                String line = scanner.nextLine();
                String[] coordinates = line.split(" ");
                if(coordinates.length == 2) {
                    String origin = coordinates[0];
                    String dest = coordinates[1];
                    if(validCoordinate(origin, dim) && validCoordinate(dest,dim)) {
                        flag = false;
                        return coordinates;
                    }
                }
                System.out.println("ERROR: Please enter valid coordinate pair separated by space.");
            }
        }
    
    0 讨论(0)
  • 2021-01-27 03:26

    Your validcoordinate is incorrect, it only checks the first few values specified by the dimension argument in the alphabet array.

    Try this

    public static boolean validCoordinate(String coordinate, int dimension) {
            boolean isValidCoordinate;
            String [] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
            int [] numbers = new int [dimension];
            for(int i = 0; i < dimension; i++){
                numbers[i] = 1 + i;
            }
            if(Arrays.asList(alphabet).contains(Character.toString(coordinate.charAt(0)))) {
                for(int j = 0; j < dimension; j++) {
                  if ((coordinate.substring(1)).contains(Integer.toString(numbers[j]))) {
                      return true;
                  }
                }
            }
            return false;
        }
    

    you can definitely improve this code and I will leave that as a challenge to you.

    here is the associated repl https://repl.it/@Blakeinstein/question60251495

    ninja edit: I suggest you to modify your positionquery

    public static String[] positionQuery(int dim) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Provide origin and destination coordinates.");
            System.out.println("Enter two positions between A1-H8:");
            while(true) {
                String line = scanner.nextLine();
                String[] coordinates = line.split(" ");
                if(coordinates.length == 2) {
                    String origin = coordinates[0];
                    String dest = coordinates[1];
                    if(validCoordinate(origin, dim) && validCoordinate(dest,dim)) {
                        return coordinates;
                    }
                    else{
                      System.out.println("Coordinates are not valid");
                    }
                }
                else{
                  System.out.println("ERROR: Please enter valid coordinate pair separated by space.");
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题