首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >改进我的Java面向对象?复习!

改进我的Java面向对象?复习!
EN

Code Review用户
提问于 2013-07-06 15:12:48
回答 2查看 373关注 0票数 4

自从然后?

之后,我有进步吗?

我今天一直在写这个程序,并尽力提高我的面向对象的理解。

Mains.java:

代码语言:javascript
复制
import games.GameHandler;
import java.util.Scanner;
import java.io.*;

    public class Mains {

        public static void main (String[] args) {
            //Start the game
            startGame();

        }

        private static void startGame() {

            //Declares
            GameHandler handler = new GameHandler();
            Scanner console = new Scanner(System.in);   
            boolean game = true;
            String input = "";  

            //Print program welcome text
            handler.printStart();

            //While in game...
            while (game) {
                //Getting input ready for new commands from the player
                input = console.nextLine();

                //Checking if input was set.
                if (input != null) {
                    //Selecting the game you want to play.
                    handler.selectGame(input);

                    //If game was selected.. then.. let's start playing.
                    while (handler.inGame) {
                        //Use will say something.
                        input = console.nextLine();

                        //If it was "exit", it will go back and select another game.
                        if (input.equals("exit")) {
                            handler.exitGame();
                        } else {
                            //Play again.
                            handler.continueGame(input);
                        }
                    }
                }
            }
        }
    }

GameHandler.java:

代码语言:javascript
复制
package games;
import java.io.*;

    public class GameHandler {

        private String[] games = {"Spin", "Tof"};
        private String[] navigation = {"Back", "Start"};
        private Spin spin = new Spin();
        private boolean spinGame = false;
        private boolean tofGame = false;
        public boolean inGame = false;

        /**
        * Method printStart
        *
        * Will welcome the player to the program.
        */
        public void printStart() {

            this.print(0, "Welcome to the program!");
            this.print(0, "Please select a game: " + this.availableGames());

        }

        /**
        * Method available games
        *
        * This will print all the games that are located in the games array in one row.
        **/

        private String availableGames() {
            String names = "";
            for (int i = 0; i < games.length; i++) {
                names = (names + games[i]);
                if (i < games.length -1) {
                    names = (names + ", ");
                }
            }

            return names;
        }

        /**
        * Method selectGame
        *
        * This will select the given game.
        * @param command The entered command.
        **/

        public void selectGame(String command) {
            if (this.inArray(command))
            {
                if (command.equalsIgnoreCase("spin")) {
                    this.startGame("spin");
                } else if (command.equalsIgnoreCase("tof")) {
                    this.startGame("tof");
                }
            } else {
                this.print(0, "Could not find game!");
            }
        }

        /**
        * Method inArray
        *
        * This will check if the entered game name is exisiting in the games array.
        * If yes, will return a boolean true, else false.
        *
        * @param value The entered game name.
        * @return boolean true/false.
        **/

        private boolean inArray(String value) {
            int returning = 0;
            for (String s : games) {
                if (value.equalsIgnoreCase(s)) {
                    returning = 1;
                }
            }
            if (returning == 1) {
                return true;
            } else {
                return false;
            }
        }

        /**
        * Method startGame
        *
        * Will start the game, and print instructions.
        * will set the game boolean to true.
        **/

        private void startGame(String game) {
            switch (game) {
                case "spin":
                    this.print(0, "Welcome to spin game!");
                    this.print(0, "Please click on any key to spin!");
                    spinGame = true;
                break;
                case "tof":
                break;
            }

            inGame = true;
        }

        /**
        * Method continueGame
        *
        * Will continue the game, either spin again, or print new question or even answer.
        * @param command The entered command.
        **/
        public void continueGame(String command) {
            while (inGame) {
                if (spinGame) {
                    this.spinWheel();
                    // Break out of the loop.
                    break;
                }
            }
        }

        /**
        * Method exitGame
        *
        * Exit the game..
        **/

        public void exitGame() {
            spinGame = false;
            tofGame = false;
            this.printStart();
        }

        /**
        * Method spinWheel
        *
        * This will spin the wheel.
        **/

        private void spinWheel() {
            this.print(0, spin.spinWheel());
        }

        /**
        * Method print
        *
        * Prints text using System.out
        * @param type printing type (Println/print).
        * @param message The message
        **/

        private void print(int type, String message) {
            switch (type) {
                case 0:
                    System.out.println(message);
                break;
                case 1:
                    System.out.print(message);
                break;              
            }
        }
    }

spin.java:

代码语言:javascript
复制
package games;
import java.util.Random;

    public class Spin {

        /**
        * The base auth we are going to work with..
        **/

        private int auth = this.rand(1000) / 5; 

        /**
        * Creating new Random object.
        **/

        private Random r = new Random();

        /**
        * Method spinWheel
        *
        * Spins the damn wheel..
        * @return spinned value + if you won or not.
        **/

        public String spinWheel() {
            return this.spinWheel(this.rand(100));
        }

        /**
        * spinWheel
        *
        * Returning results.
        **/

        private String spinWheel(int number) {

            int result = this.Calculate(this.rand(number));

            if (result < 101) {
                return "You have won the game!" + result;
            } else {
                return "You've lost the game!" + result;
            }
        }

        /**
        * Method calculate
        *
        * Calculates the spin.
        * @return the spinned number.
        **/


        private int Calculate(int Number) {

            int var = this.rand(101);

            int holder = (var * Number) / 2;

            return holder + this.auth;
        }

        /**
        * Shortcut for nextInt of Random
        **/

        public int rand(int x) {
            return r.nextInt(x);
        }

    }

它有什么问题?从上次开始我进步了吗?有什么建议吗?

EN

回答 2

Code Review用户

发布于 2013-07-06 18:30:33

我建议你改变一下

代码语言:javascript
复制
  private boolean inArray(String value) {
        int returning = 0;
        for (String s : games) {
            if (value.equalsIgnoreCase(s)) {
                returning = 1;
            }
        }
        if (returning == 1) {
            return true;
        } else {
            return false;
        }
    }

代码语言:javascript
复制
  private boolean inArray(String value) {
        boolean doesExist = false;
        for (String s : games) {
            if (value.equalsIgnoreCase(s)) {
                doesExist = true;
                break;
            }
        }
        return doesExist;
    }

从那以后的进步令人印象深刻。

把这个

代码语言:javascript
复制
public class GameHandler {

    private String[] games = {"Spin", "Tof"};
    private String[] navigation = {"Back", "Start"};
    private Spin spin = new Spin();
    private boolean spinGame = false;
    private boolean tofGame = false;
    public boolean inGame = false;

GameHandler构造函数中,在类构造函数中定义非静态变量是惯例。

您的Spin类中有一个巨大的错误。你已经声明了private int auth = this.rand(1000) / 5;。那么你知道this关键字是如何工作的吗?我不这么认为,在运行您的新代码并根据前面的代码进行判断之后,我认为您只是尝试将您的巨大代码分解成更小的部分,而不考虑如何执行它的逻辑。我将建议您首先使用小类和方法进行一些基本的Java编程,您应该从官方教程开始。祝好运。

票数 4
EN

Code Review用户

发布于 2013-07-13 22:37:10

在廷廷米回答的基础上,我会改变他的建议

代码语言:javascript
复制
private boolean inArray(String value) {
    boolean doesExist = false;
    for (String s : games) {
        if (value.equalsIgnoreCase(s)) {
            doesExist = true;
            break;
        }
    }
    return doesExist;
}

转到

代码语言:javascript
复制
private boolean inArray(String value) {
    for (String s : games)
        if (value.equalsIgnoreCase(s))
            return true;
    return false;
}

- Another减少一半。

票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

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

复制
相关文章

相似问题

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