半日闲ovo 2022-11-07 20:17 采纳率: 100%
浏览 26
已结题

对二维整数数组进行操作

问题遇到的现象和发生背景

对二维整数数组进行操作

用代码块功能插入代码,请勿粘贴截图
package lab3;
import java.util.Scanner;
public class Part2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the count of columns: ");
int columns = scanner.nextInt();
System.out.println("Enter the count of rows: ");
int rows = scanner.nextInt();
int[][] matrix = new int[rows][columns];
java.util.Random random = new java.util.Random();
for (int i = 0; i < rows; i++) 
{
for (int j = 0; j < columns; j++)
matrix[i][j] = random.nextInt(10) - 4;
}
int[] countByRows = new int[rows];

//输出二维数组
for (int i = 0; i < rows; i++) 
{
for (int j = 0; j < columns; j++)
System.out.print(matrix[i][j] + " ");
System.out.println();
}

//计数至少有一个零元素的行
int amount = 0 ;
for (int i = 0; i < rows; i++)
    if ( [rows] == 0) 
    {
        amount = amount+ 1 ;
    }
System.out.println("The number of rows with at least one zero element is" + amount);

//找出具有相同元素最多的列的列数



}
}

运行结果及报错内容

计数至少有一个零元素的行报错

我想要达到的结果

1.计数至少有一个零元素的行
2.找出具有相同元素最多的列的列数

  • 写回答

4条回答 默认 最新

  • CSDN专家-sinJack 2022-11-07 20:20
    关注
    
    package lab3;
    import java.util.Scanner;
    public class Part2 {
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter the count of columns: ");
    int columns = scanner.nextInt();
    System.out.println("Enter the count of rows: ");
    int rows = scanner.nextInt();
    int[][] matrix = new int[rows][columns];
    java.util.Random random = new java.util.Random();
    for (int i = 0; i < rows; i++) 
    {
    for (int j = 0; j < columns; j++)
    matrix[i][j] = random.nextInt(10) - 4;
    }
    int[] countByRows = new int[rows];
     
    //输出二维数组
    //计数至少有一个零元素的行
    int amount = 0 ;
    for (int i = 0; i < rows; i++) 
    {
      boolean flag=false;
      for (int j = 0; j < columns; j++){
         if(matrix[i][j]==0){
            flag=true;
        }
        System.out.print(matrix[i][j] + " ");
      }
      if(flag)
        amount = amount+ 1 ;
      System.out.println();
    }
     
    System.out.println("The number of rows with at least one zero element is" + amount);
     
    //找出具有相同元素最多的列的列数
     
     
     
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
    1人已打赏
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 11月15日
  • 已采纳回答 11月7日
  • 创建了问题 11月7日

悬赏问题

  • ¥15 音量自动减小啦!日历自动翻页
  • ¥15 微软硬件驱动认证账号申请
  • ¥15 有人知道怎么在R语言里下载Git上的miceco这个包吗
  • ¥15 GPT写作提示指令词
  • ¥20 如何在cst中建立这种螺旋扇叶结构
  • ¥20 根据动态演化博弈支付矩阵完成复制动态方程求解和演化相图分析等
  • ¥20 关于DAC输出1.000V对分辨率和精度的要求
  • ¥15 华为超融合部署环境下RedHat虚拟机分区扩容问题
  • ¥15 哪位能做百度地图导航触点播报?
  • ¥15 请问GPT语言模型怎么训练?