簡體   English   中英

如何使用 java.util.Scanner class 讀取輸入?

[英]How to use java.util.Scanner class to read input?

我有一個關於使用 java.util.Scanner class 讀取輸入的問題。

下面是我的編碼,沒有使用 java.util.Scanner class 讀取輸入:

         public class NewTry {
          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[]) {


          float[][] a = new float[][] { { 1.513f, 2.321f, 3.421f }, { 4.213f, 5.432f, 6.123f },
        { 7.214f, 8.213f, 9.213f } };



          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!!!");
    }
        }
    }

在不使用 java.util.Scanner 的情況下顯示 output:

      run:
     1.513 2.321 3.421 
     4.213 5.432 6.123 
     7.214 8.213 9.213 
     BUILD SUCCESSFUL (total time: 3 seconds)

我的問題是如何在編碼中添加 java.util.Scanner class 以讀取沒有默認浮點數的輸入? 是否使用陣列運行掃描儀?

其實我想要的樣品 output 和下面一樣(浮點數必須自己輸入):

       run:
       Type nine float numbers two-dimensional array of similar type and size with line breaks, end 
       by"-1":
       1.513
       2.321
       3.421 
       4.213
       5.432 
       6.123 
       7.214
       8.213 
       9.213 
       -1


       The result is:

       1.513 2.321 3.421 
       4.213 5.432 6.123 
       7.214 8.213 9.213 
       BUILD SUCCESSFUL (total time: 11 second) 

希望有人可以指導我或修改我的編碼以添加 java.util.Scanner class 以讀取輸入。謝謝。

看一下這個

 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!!!");
            }
        }

您應該定義一個新的 Scanner (sc),然后循環 3 x 3 次,直到您讀取所有輸入。 如果用戶輸入 -1,則循環結束。 請注意,輸入 9 個數字時無需退出。

此外,每個用戶輸入都被讀取為一行而不是一個標記,然后被解析為一個浮點數。 如果用戶輸入的字符串不是浮點數,則會引發異常。

樣本:

Type nine float numbers two-dimensional array of similar type and size with line breaks, end by -1:
1.1
1.2
1.3
2.1
2.2
2.3
3.1
3.2
3.3

The result is:
1.1 1.2 1.3 
2.1 2.2 2.3 
3.1 3.2 3.3 
Scanner scan =new Scanner(System.in);
float[][] a = new float[3][3];

for(int i =0 ;i<3;i++) {
   for(int j=0; j<3; i++) {
      a[i][j] =scan.Float();
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM