【问题标题】:Printing out columns in a 2D array打印出二维数组中的列
【发布时间】:2015-02-05 15:42:13
【问题描述】:

我的打印方法没有打印出所有的列。它只打印出一半的列,但打印出所有的行。

如何更改我的打印方法以打印出所有列?

import java.io.*;
import java.util.Scanner;

public class T_GameOfLife {
  private char[][] board;
  private int columns;
  private int rows;
  private int generation;

  public static void main(String[] args) throws FileNotFoundException {
    T_GameOfLife game = new T_GameOfLife();
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter how many generations to compute: ");
    int gen = keyboard.nextInt();
    System.out.println("Generation 1:");
    game.print();

    for (int i = 2; i <= gen; i++) {
      System.out.println("Generation " + i);
      game.computeNextGeneration(gen);
      game.printNew();
    }
  }

  public T_GameOfLife() throws FileNotFoundException {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter file name: ");
    String filename = keyboard.nextLine();
    File file = new File(filename);
    Scanner inputFile = new Scanner(file);
    int columns = inputFile.nextInt();
    int rows = inputFile.nextInt();
    inputFile.nextLine();
    board = new char[rows][columns];

    for (int i = 0; i < rows; i++) {
      String line = inputFile.nextLine();
      for (int j = 0; j < columns; j++) {
        board[i][j] = line.charAt(j);
      }
    }
  }

  public int getColumns() {
    return columns;
  }

  public int getRows() {
    return rows;
  }

  public int getCell(int rows, int columns) {
    if (board[rows][columns] == 'X' || board[rows][columns] == '0') {
      return board[rows][columns];
    } else {
      return 0;
    }
  }

  public void setCell(int rows, int columns, int value) {
    for (int i = 0; i < board.length; i++) {
      for (int j = 0; j < board[0].length; j++) {
        value = board[rows][columns];
      }
    }
  }

  public void print() {
    for (int i = 0; i < board.length; i++) {
      for (int j = 0; j < board[i].length; j++) {
        System.out.print( board[i][j] );
      }
      System.out.println();
    }
  }
}

【问题讨论】:

    标签: java


    【解决方案1】:

    您的setCell 函数似乎什么也没做。我想你的意思是写:

    board[rows][columns] = value;
    

    而不是你目前拥有的。

    就打印部分而言,您的函数正在打印出所有的行和列,因此板子可能有一半是空的或不可打印的字符。

    【讨论】:

    • 如果我这样写,那就行不通了。以下作品: value = board[rows][columns];但它做同样的事情吗?
    • @Komal 它不做同样的事情。一种是读取数组中的值并将其传递给变量,另一种是将变量的值传递给数组中的单元格。
    • 正如@JS1 提到的,该函数似乎什么也没做。
    • 删除setCell函数中的循环不会对程序进行任何更改,因为其中没有使用任何参数。
    • @McAdam331 我把语句写成 board[rows][columns] = value;但我可能会丢失精度错误。我该如何解决?
    【解决方案2】:

    根据您在其他解决方案中的有趣对话,您可能需要先查看 edx 或 coursera 的编程课程介绍,然后再继续此 Java 编程冒险......

    尽管回答你的问题..

    board[rows][columns] = value;
    

    是你想要的,否则 setcell 什么也不做。而精度的错误是指在您的值被声明为 int 时,您的电路板使用了 char,即 type 错误。

    对于打印循环,我无法推断出任何东西,但您可能想尝试使用“行”和“列”而不是 .length

    【讨论】:

      猜你喜欢
      • 2015-02-03
      • 2016-07-26
      • 1970-01-01
      • 2017-05-22
      • 2018-09-30
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      相关资源
      最近更新 更多