如何打破这个 while(true) 循环? [英] How to break this while(true) loop?
问题描述
我正在尝试打破这个循环并在两个 if 语句都为真时返回坐标.然而,循环永远不会结束.我该如何解决?
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.");
}
}
我想我在使用 validCoordinates 时遇到了问题,因为我只使用了 validCoordinates,但找不到我做错了什么.
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;
}
推荐答案
您的有效坐标不正确,它只检查由字母数组中的维度参数指定的前几个值.
Your validcoordinate is incorrect, it only checks the first few values specified by the dimension argument in the alphabet array.
试试这个
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.
这是相关的replhttps://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.");
}
}
}
这篇关于如何打破这个 while(true) 循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!