亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在數組中正確打斷?

如何在數組中正確打斷?

當年話下 2023-12-13 16:29:41
我有一個關于中斷輸入的問題,因為我的代碼輸入兩次“-1”來停止輸入,實際上我想輸入一次“-1”來停止輸入,然后顯示數組輸出。下面是我的代碼:import java.util.Scanner;public class NewTMA {    public static float[][] clone(float[][] a) throws Exception {        float b[][] = new float[a.length][a[0].length];        for (int i = 0; i < a.length; i++) {            for (int j = 0; j < a[0].length; j++) {                b[i][j] = a[i][j];            }        }        return b;    }    public static void main(String args[]) {        Scanner sc = new Scanner (System.in);        System.out.println("enter row size");        int row =  Integer.parseInt(sc.nextLine());        System.out.println("enter column size");        int column = Integer.parseInt(sc.nextLine());        System.out.println ("Type float numbers two-dimensional array of similar type and size with line break, end by -1:");        float[][] a = new float[row][column];        for (int i=0; i<row; i++) {            for (int j=0; j<column; j++) {                String line = sc.nextLine();                if ("-1".equals(line)) {                    break;                }                a[i][j]=Float.parseFloat(line);            }         }        System.out.println("\n The result is:");        try {            float b[][] = clone(a);            for (int i = 0; i < a.length; i++) {                for (int j = 0; j < a[0].length; j++) {                    System.out.print(b[i][j] + " ");                }                System.out.println();            }        } catch (Exception e) {            System.out.println("Error!!!");        }    }}下面是我的輸出:   run: enter row size 3 enter column size 2 Type float numbers two-dimensional array of similar type and size with line breaks. end by -1: 1.4 2.4 -1 -1 The result is: 1.4 2.4  0.0 0.0  0.0 0.0  BUILD SUCCESSFUL (total time: 13 seconds)實際上我只想輸入一次“-1”來停止輸入,但我不知道為什么輸出顯示兩次“-1”來停止輸入。希望有人能幫助我找出我做錯的部分。謝謝。
查看完整描述

2 回答

?
嗶嗶one

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”)


查看完整回答
反對 回復 2023-12-13
?
浮云間

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;

   }

}


查看完整回答
反對 回復 2023-12-13
  • 2 回答
  • 0 關注
  • 196 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號