[英]How to break out of while loop while continuing to iterate in nested for loop
因此我想做的是驗證我的用戶僅使用逗號輸入數字1-4,且不重復。 例如1,2,3,4或2,1,4,3。 我包括一個while循環,以確保用戶輸入正確的信息。 當涉及到for循環時,我感到困惑,因為我不確定是否需要附加的for循環才能使用i和j訪問數組中的該數組? 我真的很困惑,我正在尋求某種幫助。 我目前只有Python經驗。
我意識到,當我跳出while循環以將值輸入到數組中時,一旦返回並詢問用戶下一行的值,for循環的初始化和更新就會重新開始。
我還嘗試將if語句更改為: if (row1Values4x4.charAt(k) > '0' && row1Values4x4.charAt(k) < '5') {
但是,這沒有任何意義,因為我想突破當我的for循環遇到的值不在1-4范圍內時,即為循環。
我沒有收到任何錯誤,它沒有按照我想要的方式工作。
import java.util.Scanner;
公共課sudokuPractice {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] fourArray = { {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} };
int row = 1;
int k = 0;
while (row < 5)
{
String row1Values4x4 = "-1";
boolean done = false;
while (!done)
{
Scanner firstRow4x4 = new Scanner(System.in);
System.out.println("Please enter four values using commas for row " + row); //this needs to loop
row1Values4x4 = firstRow4x4.next();
row1Values4x4 = row1Values4x4.replaceAll(" ",""); //this is in case user enters numbers with spaces
if (row1Values4x4.length() == 7) {
for (k = 0; k < row1Values4x4.length();k++) {
if (row1Values4x4.charAt(k) < '0' && row1Values4x4.charAt(k) > '5') {
done = true;
} //else {
//}
}
}
}
String strArray[] = row1Values4x4.split(",");
int arraySidesInteger[] = new int[strArray.length];
for (int i = 0; i < strArray.length; i++) {
arraySidesInteger[i] = Integer.parseInt(strArray[i]);
}
fourArray[row-1] = arraySidesInteger;
for (int i = 0; i < fourArray.length; i++) {
for (int j = 0; j < fourArray.length; j++)
System.out.print(fourArray[i][j] + " ");
System.out.println();
}
row++;
k++;
}
}
}
//因為我試圖保留for循環的初始化和更新,所以包含了整數k。
請嘗試一下
公共靜態void main(String [] args){
int row = 1;
int k = 0;
String row1Values4x4 = "-1";
int[][] arraySidesInteger = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
while (row <= 4) {
boolean done = false;
while (!done) {
Scanner firstRow4x4 = new Scanner(System.in);
System.out.println("Please enter four values using commas for row " + row); // this needs to loop
row1Values4x4 = firstRow4x4.next();
String[] values = row1Values4x4.split(",");
for (int i = 0; i < values.length; i++) { //Logic to validate the value
if (Integer.parseInt(values[i]) < 0
|| Integer.parseInt(values[i]) > 4) {
System.out
.println("You have entered the value which not fit in the boundary.Please Try again.");
done = true;
break;
}
}
if (done) { // Loop again to get the row value correctly.
done = false;
continue;
}
//Adding the value to row
for (int i = 0; i < values.length; i++) {
arraySidesInteger[row - 1][i] = Integer.parseInt(values[i]);
}
row++;
done = true;
}
}
//Prints the array
for (int i = 0; i < arraySidesInteger.length; i++) {
for (int j = 0; j < arraySidesInteger.length; j++)
System.out.print(arraySidesInteger[i][j] + " ");
System.out.println();
}
}
如果使用標簽,中斷任何級別的循環執行都是很容易的
看例子
int count=0;
A: while(true){
while(true){
System.out.println(count++);
if(count==10){
break A;
}
}
}
第一個循環標記為A,並且break停止執行標記為A的代碼塊的執行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.