作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在努力寻找解决此问题的 Java 代码。首先,我处于初学者水平,这个问题对我来说很难回答。我知道这使用了二维数组,对吗?但是我在为这个问题编写 Java 代码时遇到问题。
问题是:
Write a Java program that
- queries a user for the number of rows and columns of a contingency table,
- read the data, row by row and
- displays the data in tabular form along with the row totals, column totals and grand total.
For example if the six data of 2x3 table are
1,3,6,7,9, and 8. the program displays these six numbers together with the appropriate totals as
1 3 6 | 10
7 9 8 | 24
8 12 14 | 34The character '|' is used to separate the data from the row totals
最佳答案
我首先建议您忘掉“数组”,多想想“类”。 Java 是一种面向对象的语言。将你想要的行为封装在一个类中。我们称它为 Tableau
。
你的类(class)至少需要四种不同的行为才能正确:
PrintStream
、FileInputStream
)将值读入 Tableau计算机科学是将复杂的任务分解成更小、更易于管理的部分。这叫做“分解”。这是学习它的好机会。
所以这是一个开始:
package model;
public class Tableau {
private int numRows;
private int numCols;
private int[][] values;
public Tableau(int numRows, int numCols) {
if (numRows <= 0) throw new IllegalArgumentException("numRows must be positive");
if (numCols <= 0) throw new IllegalArgumentException("numCols must be positive");
this.numRows = numRows;
this.numCols = numCols;
this.values = new int[numRows+1][numCols+1];
}
// You add the rest
public String toString() {
StringBuilder builder = new StringBuilder();
return builder.toString();
}
}
关于java - 列联表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10787846/
我是一名优秀的程序员,十分优秀!