试图将给定的输入存储为floyd三角形

时间:2016-10-24 07:07:46

标签: java

    import java.util.*;
    public class triangle{
      public static void main(String args[]){
        Scanner input_Obj = new Scanner(System.in);
        System.out.println("Enter the number of lines in the triangle");
        int sum = 0;
        int mat_size = input_Obj.nextInt(); //input of the size of the triangle
        System.out.println("enter the inputs");
        int input_Array[][] = new int[mat_size][mat_size];
        for (int row = 0; row < mat_size; row++){
        for (int col = 0; col < col; col++){
            input_Array[row][col] = input_Obj.nextInt();
        }
    }
        if (mat_size >= 3){
            int[] sum_array = new int[2*(mat_size-2)];
        for (int row = 1; row < mat_size; row++){
            for(int col = 0;col<=row;col++){
                sum += input_Array[row][col];  
            }
            sum_array[row-1] = sum;
        }
    }
        else if(mat_size == 2){
            if (input_Array[1][0]<input_Array[1][1]){
                System.out.println("minimum of the two elements in second      line is:" +input_Array[1][0]);
            }
            else{
                System.out.println("minimum of the two elements in second line is:" +input_Array[1][1]);
            }
        }
        else{
            System.out.println("minimum sum can't be calculated");
        }
    }
}

它不会在输入数组中存储值 它接受mat_size中的输入但不接受输入到数组中 我想找到三角形内的最小总和

1 个答案:

答案 0 :(得分:0)

for (int col = 0; col < col; col++)

col < col这是问题,因为条件永远不会满足,这就是为什么它没有接受任何输入,将此条件更改为

for (int col = 0; col < mat_size; col++)