如何在数组中正确打断?

我有一个关于中断输入的问题,因为我的代码输入两次“-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”来停止输入。希望有人能帮助我找出我做错的部分。谢谢。


当年话下
浏览 123回答 2
2回答

哔哔one

break跳出最内层循环,因此外层循环再次迭代并再次读取输入。要跳出外循环,请使用标签:outerLoop: // label the outer for loopfor (int i=0; i<row; i++){&nbsp; &nbsp; for (int j=0; j<column; j++) {&nbsp; &nbsp; &nbsp; &nbsp; String line = sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; if ("-1".equals(line)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break outerLoop; // break from the outer for loop&nbsp; &nbsp; }&nbsp; &nbsp; ...&nbsp;}您可以使用任何 Java 允许的标签名称(为了清楚起见,我将其称为“outerLoop”)

浮云间

另一种方法是放置一个标志作为参数是否满足的指示:&nbsp;for (int i=0; i<row; i++){&nbsp; &nbsp; /* this is the flag */&nbsp; &nbsp; boolean isInputNegative = false;&nbsp; &nbsp; for (int j=0; j<column; j++){&nbsp; &nbsp; &nbsp; &nbsp;String line = sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp;if ("-1".equals(line)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;isInputNegative = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;a[i][j]=Float.parseFloat(line);&nbsp; &nbsp;}&nbsp; &nbsp;/* here is the checking part */&nbsp; &nbsp;if (isInputNegative) {&nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java