Forum Stats

  • 3,727,646 Users
  • 2,245,430 Discussions


Help understanding the right code approach/structure to use in Java

edited October 2017 in New To Java

I'm just getting started learning Java. I'm working on a simple console game to learn as I go, and I'd like to get a better understanding of how I should structure my code (see below).

So far, I've just been writing it in a linear fashion as I logically think through each step, but I suspect this is not the right way to proceed in Java. I get the feeling that I should be splitting my code into various classes, objects, methods, etc?

How would you structure the below code (keeping in mind I'm a beginner, so simple is better.)?

Eg should I create a separate class/object for character and use that to set/store the name, race, & stats?

Then create classes, constructors, and methods for each of the game actions? Eg a class and object for Beer, and a drink method.

import java.util.Scanner;import java.util.Random;public class LOTRGame {   /**    * @param args the command line arguments    */   public static void main(String[] args) {       System.out.printf("Welcome, traveler, to the Green Dragon Inn! What's your name? %n");       Scanner scanIn = new Scanner(;       String characterName = scanIn.nextLine();       boolean invalidRace;       do {       System.out.printf("Come on in, %s. Pardon me for asking, but what race are you? %n", characterName);       String characterRace = scanIn.nextLine();       invalidRace = (!characterRace.equalsIgnoreCase("elf") && !characterRace.equalsIgnoreCase("human") && !characterRace.equalsIgnoreCase("hobbit") && !characterRace.equalsIgnoreCase("wizard") && !characterRace.equalsIgnoreCase("dwarf"));        if (invalidRace) {          System.out.printf("Never heard of that.%n");        }        else {                           System.out.printf("'Tis a windy night out! Step on in by the fireplace, %s. We're always happy to welcome a friendly %s to our company!%n", characterName, characterRace);        }       } while (invalidRace);       Random rand = new Random();int coins = rand.nextInt(30) + 10;int health = 90;int energy = 50;String doNext;boolean goOn;do {System.out.printf("---You have %s gold coins, %s health, and %s energy.---%n%nWhat would you like to do?%n---Your options are Beer, Gamble, Dance, Or Sleep---%n", Integer.toString(coins), Integer.toString(health), Integer.toString(energy));doNext = scanIn.nextLine();   goOn = (!doNext.equalsIgnoreCase("Sleep"));if (doNext.equalsIgnoreCase("Beer")) {  coins = --coins;  health = health - 5;  energy = energy + 2;}else if (doNext.equalsIgnoreCase("Gamble")) {   System.out.printf("How many coins do you want to bet?%n");   String betCoins = scanIn.nextLine();   int bet = Integer.parseInt(betCoins);   Random randBet = new Random();   int randBetResult = randBet.nextInt(2) + 1;   if (randBetResult == 1) {  System.out.printf("You lose! -%s coins.%n", betCoins);  coins = coins - bet;}   else {  System.out.printf("You win! +%s coins.%n", betCoins);  coins = coins + bet;}}else if (doNext.equalsIgnoreCase("Dance")) {  System.out.printf("Nice dancing! Here's a coin for a beer on the house.");  coins = ++coins;  energy = energy - 2;}else if (doNext.equalsIgnoreCase("Sleep")) {System.out.printf("Good night!%n");exit(0)}} while (goOn);  scanIn.close();     }   }


  • Unknown
    edited October 2017
    I'm just getting started learning Java. I'm working on a simple console game to learn as I go, and I'd like to get a better understanding of how I should structure my code (see below).

    The BEST way to 'get a better understanding' is to learn to do it the right way from the beginning.

    I suggest you start working your way through The Java Tutorials

    There are trails that cover ALL of the basic Java functionality and hundreds of examples with working code.

    1. read a trail

    2. try the example(s) the trail providesk

    3. try simple modifications to the example and see what effect they have

    Initially just scan the trails and read the first paragraph to see what the trail is about and what it is trying to teach. Then you will get a sense of where you want to start.

    Otherwise  you are just asking for people to tear your code apart, bit by bit. That won't give you any sense at all of how you should structure a program or write MODULAR code.

    Download and learn how to use a GUI (e.g. NetBeans) to manage your project and code.

  • morgalr
    morgalr Member Posts: 457
    edited October 2017

    You have the basic linear functional programming model, and you are correct in assuming that you should be breaking things out into classes and methods, but before you can do that you need to go through the tutorials as already suggested rp0428.

    You are trying to program Java in the manner you already know programming, and it's not Object Oriented Programming.  You are putting everything in your main method which makes you program execution basically linear functional program.  First thing to notice in the tutorial is that your main is just the entry point of your program/project and not your "main work area". Take a look how that problem is solved in the tutorials; the sooner you get way from what you are doing, the better you'll enjoy programming in Java.


This discussion has been closed.