2 回答

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個贊
break跳出最內(nèi)層循環(huán),因此外層循環(huán)再次迭代并再次讀取輸入。
要跳出外循環(huán),請使用標(biāo)簽:
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 允許的標(biāo)簽名稱(為了清楚起見,我將其稱為“outerLoop”)

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超4個贊
另一種方法是放置一個標(biāo)志作為參數(shù)是否滿足的指示:
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;
}
}
添加回答
舉報