【问题标题】:How stop array input limit then the result to show the output?如何停止数组输入限制然后结果显示输出?
【发布时间】:2019-10-24 23:37:34
【问题描述】:

我有一个关于停止数组输入限制的问题,然后是显示输出的结果。

下面是我的编码已经设置了新的float[3][3]

    import java.util.Scanner;


      public class Clone2Darray {
    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 ("Type nine float numbers two-dimensional array of similar type and size with line breaks, end by -1:");
         float[][] a = new float[3][3];
          for (int i=0; i<3; i++){
             for (int j=0; j<3; 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:
 Type float numbers in the two-dimensional array of similar type and size 
  with line breaks, end by -1:

  5.33
  9.33
  63.33
  6.36
  3.55
  7.25
  2.33
  3.66

  The result is:
  6.33 5.33 9.33 
  63.33 6.36 3.55 
  7.25 2.33 3.66 
  BUILD SUCCESSFUL (total time: 31 seconds)

我的问题是要停止限制float[3][3] 并且可以无限制地键入输入,直到键入 -1 以停止输入。我可以知道如何删除数组中的限制 float[3][3] 吗?希望任何人都可以指导我解决我的问题。谢谢。

【问题讨论】:

    标签: java arrays


    【解决方案1】:

    在为二维数组分配内存时,您必须告知其元素的大小,因为将为该数组分配内存并且必须知道要分配的内存量。

    您可以通过使用一些更动态的类型(例如List 及其流行的实现ArrayList)来绕过这一点,即使是嵌套形式。这是一件好事,但是你不会有一个“真正的”数组。

    【讨论】:

    • 你能编辑我的代码来告诉我它是如何工作的吗?
    • @CHSon 为此,我需要知道您打算如何做。我不了解您的确切目标,但我了解您不想将自己与尺寸决定联系起来。你可以在这里看到一个关于 ArrayList 的示例:geeksforgeeks.org/arraylist-of-arraylist-in-java 我想这离你的目​​标不是很远。
    【解决方案2】:

    以下代码允许您创建动态数组。

    
    import java.util.Scanner;
    
    
    public class Clone2DArray {
    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 breaks:");
       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!!!");
      }
       }
    }
    
    

    【讨论】:

    • 但是为什么要按2次-1然后停止输入呢?
    猜你喜欢
    • 2020-01-28
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 2012-04-25
    • 1970-01-01
    相关资源
    最近更新 更多