2015-10-07 28 views
-1

我將如何通過添加數組的最小值以及最小值所在的位置來完成此程序?我將如何找到這個數組索引的位置,也是最小值?

public static void main(String[] args) { 
    Scanner input; 
    /* A file for the program to open, the absolute location is based on 
    * the location of the project. /src/array2d/ locates the file in 
    * the current source folder 
    */ 

    File fileIn = new File("src/array2d/array2dtest1.txt"); 
    // You can fetch the full absolute path with the method below 
    // System.out.println(fileIn.getAbsolutePath()); 

    /* try...catch is necessary for reading files, as it is possible that 
    * the file does not exist. 
    */ 

    try { 
     //creating a scanner from the file, rather than using console. 
     input = new Scanner(fileIn); 
    } 
    catch (FileNotFoundException e) { 
     // if file is not found, terminate the program 
     System.out.println(fileIn.getName() + " is not found."); 
     return; 
    } 

    int row = input.nextInt(); 
    int column = input.nextInt(); 
    int [][] arr = new int[row][column]; 
    int [] min = arr[0]; 

    for (int i = 0; i < row; i++) { 
     for (int j = 0; j < column; j++) { 
      arr[i][j] = input.nextInt(); 
     } 
    } 

    for (int i = 0; i < row; i++) { 
     for (int j = 0; j < column; j++) { 
      int k; 
      k = arr[i][j]; 
      System.out.printf("  %3d", k); 
     } 
     System.out.println(); 
    } 
    input.close(); 
    //min 
    int i; 
    for(i = 1; i < arr.length; i++) { 
     if(i == 1) 
     min = arr[i]; 
    } 
    System.out.printf("   min: " + min); 
} 

輸出應該是:

39 95 99 56 41 
88 8 1 48 75 
3 58 13 54 80 
92 72 74 25 86 
30 38 3 21 2 

最小爲1,其地位(無論位置)

+0

你的問題和代碼是不明確的。你可以添加輸入文件包含的數據嗎? – Kesavan

回答

2

下面是一個新min圈對你來說,塞到循環,既行和列,以及一些更好的格式爲您的字符串:)

int min = 0; /* set initial minimum */ 
    int minRowPos = 0; /* set minimum row and column positions */ 
    int minColPos = 0; 
    for(int i = 0; i < row; i++) 
    { 
     for(int j = 0; j < column; j++) 
     { 
      int k; 
      k = arr[i][j]; 
      System.out.printf("  %3d", k); 
      if(min < arr[i][j]){ /* test and set new min across arr */ 
       min = arr[i][j]; 
       minRowPos = i; /* set row position of new minimum */ 
       minColPos = j; /* set col position of new minimum */ 
      } 
     } 
     System.out.println(); 
     System.out.printf("Array min: %d at row, column: %d,%d ", min, minRowPos, minColPos); 

    } 
    input.close(); 

也,刪除你在頂部

int [] min = arr[0]; 

你可能會更清潔,並將所有聲明的類的頂部,如果你喜歡的min的聲明,但我不想添亂的東西了任何更多,並保持一小塊變化。

+0

即時獲得一個錯誤。它說如果(min lulprodigy

+1

如果您對它解決了您的問題感到滿意,請將答案標記爲完整以清除它並幫助他人找到答案。 –

0
//min 
int i; 
int pos; 
for(i = 1; i < arr.length; i++) { 
    if(i == 1) 
    min = arr[i]; 
    pos = i; 
} 
System.out.printf("   min: " + min + " position " + pos); 
0

首先你的代碼int [] min = arr[0];應該給你一個錯誤,因爲你不能有類型爲int []引用的參考變量爲int。

我們答案:

<java> 
int minimum = arr[0]; 
int minimum_index = 0 
for(int i = 1;i<arr.length;i++){ 
    if(arr[i]<minimum){  // Found an array element lesser than previously recorded minimum 
     minimum = arr[i]; //update the recorded minimum 
     minimum_index = i; // update the recorded minimum index 
     } 
} 
System.out.printf("Array minimum %d found at %d",minimum,minimum_index); 

相關問題