网格和菜单进入了程序
我将类集成到了组合中。
现在,程序将询问您希望网格有多大。
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
class NumberGrid {
private static final int RANDOM_NUMBER = 10;
static Random rand = new Random();
public int numberColumns;
public int numberRows;
int [][] grid;
public NumberGrid (int chosenNumberRows,int chosenNumberColumns ){
numberColumns = chosenNumberColumns;
numberRows = chosenNumberRows;
grid = new int [numberRows][numberColumns];
}
private static int randomInt(int from, int to) {
return rand.nextInt(to - from + 1) + from;
}
public void amountOfSpecificNumbers() {
int[] numbers = new int[11];
for (int y = 0; y < numberRows; y++) {
for (int x = 0; x < numberColumns; x++) {
int value = grid[y][x];
numbers[value]++;
}
}
for (int i = 1; i < numbers.length; i++) {
System.out.println(" " + numbers[i] + " " + i + "s" );
}
}
public void sumOfColumns() {
int sumOfColumns[] = new int[numberColumns];
for (int x = 0; x < numberColumns; x++) {
for (int y = 0; y < numberRows; y++) {
sumOfColumns[x] += grid[y][x];
}
}
System.out.println(Arrays.toString(sumOfColumns));
}
public void sumOfRows() {
int sumOfRows[] = new int[numberRows];
for (int y = 0; y < numberRows; y++) {
for (int x = 0; x < numberColumns; x++) {
sumOfRows[y] += grid[y][x];
}
}
System.out.println(Arrays.toString(sumOfRows));
}
public void newField() {
for (int x = 0; x < numberColumns; x++) {
for (int y = 0; y < numberRows; y++) {
int randomNumber = (randomInt(1, RANDOM_NUMBER));
grid[y][x] = randomNumber;
}
}
}
public void showField() {
for (int y = 0; y < numberRows; y++) {
for (int x = 0; x < numberColumns; x++) {
if (grid[y][x] < 10) {
System.out.print(" " + grid[y][x] + " ");
} else {
System.out.print(grid[y][x] + " ");
}
}
System.out.println();
}
}
public static int readInt(Scanner scanner){
String prompt = ("Pleas enter number 1, 2, 3, 4, 5, or 6");
System.out.println(prompt);
while(!scanner.hasNext("[1-6]")) {
System.out.println(prompt);
scanner.next();
}
return scanner.nextInt();
}
}
class GridColumns {
public static int readInt(Scanner scanner){
String prompt = ("Pleas enter the number of columns you want");
System.out.println(prompt);
while(!scanner.hasNextInt()) {
System.out.println("That's not a number");
System.out.println(prompt);
scanner.next();
}
return scanner.nextInt();
}
}
class GridRows{
public static int readInt(Scanner scanner){
String prompt = ("Pleas enter the number of rows you want");
System.out.println(prompt);
while(!scanner.hasNextInt()) {
System.out.println("That's not a number");
System.out.println(prompt);
scanner.next();
}
return scanner.nextInt();
}
}
public class TenXTen {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
NumberGrid numberGrid = new NumberGrid(GridRows.readInt(scanner),GridColumns.readInt(scanner));
numberGrid.newField();
numberGrid.showField();
while(true) {
System.out.println("What do you want to do?");
System.out.println("1. Get a new field");
System.out.println("2. Show current field");
System.out.println("3. Count the numbers in the current field");
System.out.println("4. Sum all rows");
System.out.println("5. Sum all columns");
System.out.println("6. Exit program");
int choice = numberGrid.readInt(scanner);
switch (choice) {
case 1:
numberGrid.newField();
numberGrid.showField();
break;
case 2:
numberGrid.showField();
break;
case 3:
numberGrid.amountOfSpecificNumbers();
break;
case 4:
numberGrid.sumOfRows();
break;
case 5:
numberGrid.sumOfColumns();
break;
case 6:
return;
}
}
}
}
#1 楼
字段可见性尽可能限制字段可见性。尽可能在任何地方使用
private final
(编译器会告知您是否可以)。在构造函数中使用
this.
在构造函数中,您可以使用
this.
分配您的字段。这将允许您使用与字段名称相同的参数名称。例如:public NumberGrid(int numberRows, int numberColumns) {
this.numberColumns = numberColumns;
this.numberRows = numberRows;
this.grid = new int[numberRows][numberColumns];
}
使用常量
在
amountOfSpecificNumbers()
中:int[] numbers = new int[11];
使
int[] numbers = new int[RANDOM_NUMBER + 1];
不必要的括号
您在这些行上使用了多余的括号:
int randomNumber = (randomInt(1, RANDOM_NUMBER));
String prompt = ("Pleas enter number 1, 2, 3, 4, 5, or 6");
String prompt = ("Pleas enter the number of columns you want");
那些可以成为:
int randomNumber = randomInt(1, RANDOM_NUMBER);
String prompt = "Pleas enter number 1, 2, 3, 4, 5, or 6";
String prompt = "Pleas enter the number of columns you want";
分散的
readInt
方法当前有
NumberGrid.readInt
,GridColumns.readInt
和GridRows.readInt
。NumberGrid.readInt
方法与NumberGrid
直接无关,它是一种用于控制程序流的方法。我将此方法放在TenXTen
类中。我会将此方法命名为readMenuChoice
GridColumns.readInt
和GridRows.readInt
不需要在自己的类中。它们也属于TenXTen
类。我将这些方法命名为readColumnsCount
和readRowsCount
(或使用@Pimgd的建议并将这两种方法合并为一个-但仍将它们作为static
方法放在TenXTen
内)。#2 楼
private static final int RANDOM_NUMBER = 10;
每个人都知道真正的随机数是4。
开玩笑的说,
RANDOM_NUMBER
是干什么用的?考虑添加注释来解释变量的用途...或者最好将其重命名(也许重命名为RANDOM_GRIDVALUE_MAX
。)。static Random rand = new Random();
public int numberColumns;
public int numberRows;
int [][] grid;
您缺少某些变量的可见性修饰符。
另外,
rand
可以是final
,因为它只设置了一次,然后再也没有设置。也许(提示:有)其他一些变量也可能是final
。public NumberGrid (int chosenNumberRows,int chosenNumberColumns ){
private static int randomInt(int from, int to) {
检查IDE是否具有一致的语法格式。例如,对于Eclipse:
Ctrl + Shift + F
或者,在主菜单>“源”>“格式”
String prompt = ("Pleas enter number 1, 2, 3, 4, 5, or 6");
Typo(“请”->“请”)。
public void showField() {
for (int y = 0; y < numberRows; y++) {
for (int x = 0; x < numberColumns; x++) {
if (grid[y][x] < 10) {
System.out.print(" " + grid[y][x] + " ");
} else {
System.out.print(grid[y][x] + " ");
}
}
System.out.println();
}
}
如果要打印SPACE +单元格+ SPACE,请输入小于10的值;如果不是,请打印Cell + SPACE。 。
这对我来说还不清楚...直到我意识到它是用于填充的。
我建议将if语句移至单独的函数,例如
printNumberSpacePadded(int number, int toLength)
。class GridColumns {
public static int readInt(Scanner scanner){
String prompt = ("Pleas enter the number of columns you want");
System.out.println(prompt);
while(!scanner.hasNextInt()) {
System.out.println("That's not a number");
System.out.println(prompt);
scanner.next();
}
return scanner.nextInt();
}
}
class GridRows{
public static int readInt(Scanner scanner){
String prompt = ("Pleas enter the number of rows you want");
System.out.println(prompt);
while(!scanner.hasNextInt()) {
System.out.println("That's not a number");
System.out.println(prompt);
scanner.next();
}
return scanner.nextInt();
}
}
你好重复。
遇到
readInt
:public static int readInt(Scanner scanner, String inputMessage, String errorMessage) {
System.out.println(inputMessage);
while(!scanner.hasNextInt()) {
System.out.println(errorMessage);
System.out.println(inputMessage);
scanner.next();
}
return scanner.nextInt();
}
把它粘在某个地方,也许放在
ScannerUtils
或InputHandler
类中(后者,我将负责所有输入)。但是,等等,它不支持该版本:
public static int readInt(Scanner scanner){
String prompt = ("Pleas enter number 1, 2, 3, 4, 5, or 6");
System.out.println(prompt);
while(!scanner.hasNext("[1-6]")) {
System.out.println(prompt);
scanner.next();
}
return scanner.nextInt();
}
为此,只需添加一个
String validInputPattern
或诸如此类。我认为您还不了解类。也许您确实做到了,但是只是从功能上讲。
您拥有的类不包含变量,并且没有任何非静态方法。因此,它们只不过是实用程序框而已。
NumberGrid
很好。最后...最后的菜单,我将其重写为使用lambda的以及其他一些巧妙的技巧,这样我就可以说出这样的话:
addMenuItem("Show current field", () -> numberGrid.showField());
然后就可以自己在其前面加上“ 2”。
也许这是一个坏主意,但我认为值得尝试。我不喜欢
case 5:
的意思是numberGrid.sumOfColumns()
,因为这里有双重重复... System.out.println("5. Sum all columns");
5个重复case 5:
和System.out.println("5. Sum all columns");
“所有列的总和”重复numberGrid.sumOfColumns();
。现在这种复制形式还不错,只是在菜单和操作之间添加行时,很难验证代码是否按预期工作。
评论
好的,就是这样。在“ 2015最佳代码评论-最佳标题类别”的前提下,我正在此处进行记录;)@ Mat'sMug奖励积分是由作者自己命名的,而不是我们重命名问题