问题
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 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:");
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;
}
}
System.out.println("ERROR: Please enter valid coordinate pair separated by space.");
}
}
I guess I have a problem with validCoordinates, because I am only getting true with validCoordinates, but couldn't find what I am doing wrong.
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];
int one = 1;
for(int i = 0; i < dimension; i++){
numbers[i] = one + i;
}
for(int i = 0; i < dimension; i++){
if((Character.toString(coordinate.charAt(0))).contains(alphabet[i])) {
for(int j = 0; j < dimension; j++) {
if ((coordinate.substring(1)).contains(Integer.toString(numbers[j]))) {
return true;
}
}
}
}
return false;
}
回答1:
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.");
}
}
回答2:
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.");
}
}
}
来源:https://stackoverflow.com/questions/60251495/how-to-break-this-whiletrue-loop