首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >表示滑块的简单练习

表示滑块的简单练习
EN

Code Review用户
提问于 2012-02-29 05:10:14
回答 1查看 3.1K关注 0票数 3

我一直在帮助一个学生做练习,快速输入这些内容,并认为这将是改进我自己的编程风格和解决问题的方法的理想机会。

在这个练习中,网格代表了其中一个幻灯片谜题。这些模式已被数字所取代。零表示允许移动的缺失部分。到目前为止还没有游戏,目的是简单地交换丢失的部分,就像游戏开始前所做的那样。

  1. 我怎样才能使代码更简洁?
  2. 将代码分解为函数的好方法是什么?
  3. 在Java中,还可以改进哪些东西来接近一种体面的编程风格?
代码语言:javascript
复制
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Puzzle {

    public static void main(String[] args) {

        System.out.println("Please enter dimensions of the puzzle.");

        System.out.println("Enter length (>0) ");

        Scanner sc = new Scanner(System.in);
        int width = sc.nextInt();

        System.out.println("Enter height (>0)");

        int height = sc.nextInt();

        int[][] puzzle = new int[width][height];

        populatePuzzle( puzzle );

        System.out.println("Enter position of missing piece");

        int position = sc.nextInt();

        placeMissingPiece( position, puzzle );

        System.out.println( "Missing piece is at " + Arrays.toString( findNumber( puzzle, 0 ) ) );

        displayPuzzle( puzzle );

        int number = 0;

        while( true ){
            System.out.println("Enter the number to be swapped around");

            number = sc.nextInt();

            swapElementValues( puzzle, findNumber( puzzle, 0 ), findNumber( puzzle, number ) );

            displayPuzzle( puzzle );
        }
    }

    public static void swapElementValues( int[][] puzzle, int[] missing, int[] target ){

        int tempValue = puzzle[ missing[0] ][ missing[1] ];

        puzzle[ missing[0] ][ missing[1] ] = puzzle[ target[0] ][ target[1] ];
        puzzle[ target[0] ][ target[1] ] = tempValue;
    }

    public static int[] findNumber( int[][] puzzle, int number ){

        for( int i = 0; i < puzzle.length; i++ ){
            for( int j = 0; j < puzzle[0].length; j++ ){
                if( puzzle[i][j] == number ){
                    int[] foundCoords = new int[2];

                    foundCoords[0] = i;
                    foundCoords[1] = j;

                    return foundCoords;
                }
            }
        }

        return null;
    }

    public static int getRandomInteger(){

        Random randomGenerator = new Random();

        return randomGenerator.nextInt(99) + 1;
    }

    public static void populatePuzzle( int[][] puzzle ){

        for( int i = 0; i < puzzle.length; i++ ){
            for( int j = 0; j < puzzle[0].length; j++ ){                
                puzzle[i][j] = getRandomInteger();
            }
        }

    }

    public static void displayPuzzle( int[][] puzzle ){

        for( int i = 0; i < puzzle.length; i++ ){
            for( int j = 0; j < puzzle[0].length; j++ ){
                System.out.print( puzzle[i][j] + "," );
            }

            System.out.println( "" ); // Carriage return
        }

    }

    public static void placeMissingPiece( int elementNumber, int[][] puzzle ){

        int counter = 0;

        for( int i = 0; i < puzzle.length; i++ ){
            for( int j = 0; j < puzzle[0].length; j++ ){
                counter++;

                if( counter == elementNumber ){
                    puzzle[i][j] = 0;
                }
            }
        }

    }
}
EN

回答 1

Code Review用户

回答已采纳

发布于 2012-02-29 08:01:30

代码语言:javascript
复制
        System.out.println("Enter height (>0)");

        int height = sc.nextInt();

此模式在代码中重复多次。将其转换为方法,例如(假设Scanner被转换为静态字段):

代码语言:javascript
复制
private static int promptForInt(String message) {
    System.out.println(message);
    return sc.nextInt();
}

-

代码语言:javascript
复制
    public static int getRandomInteger(){

        Random randomGenerator = new Random();

        return randomGenerator.nextInt(99) + 1;
    }

从性能的角度来看,为每个调用创建一个新的Random是没有意义的。应该只创建一次对象。

代码语言:javascript
复制
    public static void populatePuzzle( int[][] puzzle ){

    public static void displayPuzzle( int[][] puzzle ){

    public static void placeMissingPiece( int elementNumber, int[][] puzzle ){

你不是在想面向对象的东西。上面的模式非常明显地表明,每个方法都应该属于一个Puzzle类。这样,您的main方法就可以关注程序的核心逻辑,而不是谜题的实现细节。例如:

代码语言:javascript
复制
Puzzle puzzle = new Puzzle(); // .populate() should probably be called from the constructor
// ...
puzzle.placeMissingPiece(position);
// ...
puzzle.display();
// ...
puzzle.swapElementValues(0, number); // findNumber() called as a private method
// etc...
票数 8
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/9533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档