首页 > 解决方案 > 无法从数组中选择一个元素并将其分配给矩阵中的一个点

问题描述

我正在尝试从数组中选择一个元素并将其分配给矩阵中的一个点,但是每次我尝试它时都会发送此错误消息错误:不兼容的类型:可能有损从双精度转换为整数

public CountLetters(int rows, int cols, String[] vals)
    {
    char matrix[][] = new char[rows][cols];
    
    char[] source = {'a','b','c','d','e','f','g'};
     
    for(int i = 0; i < cols; i++){      
            for(int j = 0; j < rows; j++){    
             matrix[i][j] = (int)source[Math.floor(Math.random()*source.length)];    
            }        
        }  
  }

标签: javaarraysmatrixtype-conversion

解决方案


尝试这个

public void countLetters(int rows, int cols)
{
    char matrix[][] = new char[rows][cols];

    char[] source = {'a','b','c','d','e','f','g'};

    for(int i = 0; i < cols; i++){
        for(int j = 0; j < rows; j++){
            matrix[i][j] = source[(int)Math.floor(Math.random()*source.length)];
        }
    }
}

推荐阅读