2 回答

TA貢獻1854條經驗 獲得超8個贊
break跳出最內層循環,因此外層循環再次迭代并再次讀取輸入。
要跳出外循環,請使用標簽:
outerLoop: // label the outer for loop
for (int i=0; i<row; i++){
for (int j=0; j<column; j++) {
String line = sc.nextLine();
if ("-1".equals(line)) {
break outerLoop; // break from the outer for loop
}
...
}
您可以使用任何 Java 允許的標簽名稱(為了清楚起見,我將其稱為“outerLoop”)

TA貢獻1829條經驗 獲得超4個贊
另一種方法是放置一個標志作為參數是否滿足的指示:
for (int i=0; i<row; i++){
/* this is the flag */
boolean isInputNegative = false;
for (int j=0; j<column; j++){
String line = sc.nextLine();
if ("-1".equals(line)){
isInputNegative = true;
break;
}
a[i][j]=Float.parseFloat(line);
}
/* here is the checking part */
if (isInputNegative) {
break;
}
}
添加回答
舉報