Skip to main content
added 1928 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Is there any way to improve my java ? Anything wrong with it? Simple spin game

I see many many programs that are written out of the main method. But But most of my programs are written in main(), and access some methods from other classes.

Basically, this program is a simple spin game:

import java.util.Scanner;
import games.Spin;

public class Mains {
    //Declaring Scanner object as console.
    static Scanner console = new Scanner(System.in);
    //Declaring Spin game object as spin
    static Spin spin = new Spin();
    
    //Available games array
    static String[] content = {"spin", "tof"};
    //Navigation options array
    static String[] navigateGame = {"start", "back"};
    
    public static void main (String[] args) {
        
        // Setting the default number for inputNumber
        int inputNumber = 0;
        
        // Setting the defult value for inputString
        String inputString = "";
        
        // The main game loop
        boolean game = true;
        
        // Spin game loop
        boolean spinGame = false;
        
        // Truth or false game loop
        boolean tofGame = false;            
        
        System.out.print("Welcome! Please select a game: ");
        for (int i = 0; i < content.length; i++) {
            System.out.print(content[i]);
            if (i < content.length -1) {
                System.out.print(", ");
            }
        }
        
        //New line..
        System.out.println();
        
        //The main game loop
        while (game) {
        
            //This is the input we will enter to the console.
            inputString = console.nextLine();
            
            //If we are not in any of the games..
            if (!spinGame && !tofGame) {
                //Looping through the array
                for (String s : content) {
                    //If the entered input, contains any word that is in the array, enter switch statement, to find out which word was it.
                    if (inputString.equalsIgnoreCase(s)) {
                        //Entering switch statement to find out which of the values matched in the array.
                        switch (inputString) {
                            //Spin game match
                            case "spin":
                                //Start spin game
                                spinGame = true;
                            break;
                            
                            //Tof game match.
                            case "tof":
                                //Start tof game
                                tofGame = true;                        
                            break;                            
                        }
                        //Getting out of the for loop, so we won't get could not find game error after match found.
                        break;
                    } else {
                        //No match found, throw error.
                        System.out.println("Could not find game!");
                        //Break out of the loop.
                        break;
                    }
                }                
            }
            
            /*
            * Spin Game Start
            */
            
            
            //Setting up the text once user entered the game.
            
            if (spinGame) {
                System.out.println("Welcome to the spin game!");
                System.out.println("Please enter 'start' to start and 'back' to go back.");
            }
            
            /*
            * Main Spin Game loop
            */
            
            while (spinGame) {
            
                //This is the input we will pass the console.
                inputString = console.nextLine();
                
                //Looping through the array for selected navigation option.
                for (String s : navigateGame) {
                    //Checking if the entered value contains any word inside the navgiation options array.
                    if (inputString.equalsIgnoreCase(s)) {
                        //Entering the switch statement to find out what word.
                        switch (inputString) {
                            
                            //Case start game
                            //This will spin the wheel once, and tell results.
                            case "start":
                                //Printing the spinned value frm spinWheel().
                                System.out.println(spin.spinWheel());
                                //Setting the console to receive more inputs.
                                inputString = console.nextLine();
                            break;
                            
                            //Case back
                            //This will go back to the lobby.
                            case "back":
                                //Quit spinGame.
                                spinGame = false;
                                //Back to the top of the method.
                                main(args);
                            break;
                        }
                    }
                }
            }
        }
    }
}

I did not make the spin part, because I really need to know if I am doing goodwell. Because I see most of the people are doing things in the different way, I hate sticking with a one way while I think it's incorrect.

I started learning java a week ago,Is there anything wrong with my way of coding and kinda liking it. Anything wrong with my way of coding and handling?handling?

Is there any way to improve my java ? Anything wrong with it?

I see many many programs that are written out of the main method. But most of my programs are written in main, and access some methods from other classes.

Basically, this program is a simple spin game:

import java.util.Scanner;
import games.Spin;

public class Mains {
    //Declaring Scanner object as console.
    static Scanner console = new Scanner(System.in);
    //Declaring Spin game object as spin
    static Spin spin = new Spin();
    
    //Available games array
    static String[] content = {"spin", "tof"};
    //Navigation options array
    static String[] navigateGame = {"start", "back"};
    
    public static void main (String[] args) {
        
        // Setting the default number for inputNumber
        int inputNumber = 0;
        
        // Setting the defult value for inputString
        String inputString = "";
        
        // The main game loop
        boolean game = true;
        
        // Spin game loop
        boolean spinGame = false;
        
        // Truth or false game loop
        boolean tofGame = false;            
        
        System.out.print("Welcome! Please select a game: ");
        for (int i = 0; i < content.length; i++) {
            System.out.print(content[i]);
            if (i < content.length -1) {
                System.out.print(", ");
            }
        }
        
        //New line..
        System.out.println();
        
        //The main game loop
        while (game) {
        
            //This is the input we will enter to the console.
            inputString = console.nextLine();
            
            //If we are not in any of the games..
            if (!spinGame && !tofGame) {
                //Looping through the array
                for (String s : content) {
                    //If the entered input, contains any word that is in the array, enter switch statement, to find out which word was it.
                    if (inputString.equalsIgnoreCase(s)) {
                        //Entering switch statement to find out which of the values matched in the array.
                        switch (inputString) {
                            //Spin game match
                            case "spin":
                                //Start spin game
                                spinGame = true;
                            break;
                            
                            //Tof game match.
                            case "tof":
                                //Start tof game
                                tofGame = true;                     
                            break;                          
                        }
                        //Getting out of the for loop, so we won't get could not find game error after match found.
                        break;
                    } else {
                        //No match found, throw error.
                        System.out.println("Could not find game!");
                        //Break out of the loop.
                        break;
                    }
                }               
            }
            
            /*
            * Spin Game Start
            */
            
            
            //Setting up the text once user entered the game.
            
            if (spinGame) {
                System.out.println("Welcome to the spin game!");
                System.out.println("Please enter 'start' to start and 'back' to go back.");
            }
            
            /*
            * Main Spin Game loop
            */
            
            while (spinGame) {
            
                //This is the input we will pass the console.
                inputString = console.nextLine();
                
                //Looping through the array for selected navigation option.
                for (String s : navigateGame) {
                    //Checking if the entered value contains any word inside the navgiation options array.
                    if (inputString.equalsIgnoreCase(s)) {
                        //Entering the switch statement to find out what word.
                        switch (inputString) {
                            
                            //Case start game
                            //This will spin the wheel once, and tell results.
                            case "start":
                                //Printing the spinned value frm spinWheel().
                                System.out.println(spin.spinWheel());
                                //Setting the console to receive more inputs.
                                inputString = console.nextLine();
                            break;
                            
                            //Case back
                            //This will go back to the lobby.
                            case "back":
                                //Quit spinGame.
                                spinGame = false;
                                //Back to the top of the method.
                                main(args);
                            break;
                        }
                    }
                }
            }
        }
    }
}

I did not make the spin part, because I really need to know if I am doing good. Because I see most of the people are doing things in the different way, I hate sticking with a one way while I think it's incorrect.

I started learning java a week ago, and kinda liking it. Anything wrong with my way of coding and handling?

Simple spin game

I see many many programs that are written out of the main method. But most of my programs are written in main() and access some methods from other classes.

import java.util.Scanner;
import games.Spin;

public class Mains {
    //Declaring Scanner object as console.
    static Scanner console = new Scanner(System.in);
    //Declaring Spin game object as spin
    static Spin spin = new Spin();
    
    //Available games array
    static String[] content = {"spin", "tof"};
    //Navigation options array
    static String[] navigateGame = {"start", "back"};
    
    public static void main (String[] args) {
        
        // Setting the default number for inputNumber
        int inputNumber = 0;
        
        // Setting the defult value for inputString
        String inputString = "";
        
        // The main game loop
        boolean game = true;
        
        // Spin game loop
        boolean spinGame = false;
        
        // Truth or false game loop
        boolean tofGame = false;            
        
        System.out.print("Welcome! Please select a game: ");
        for (int i = 0; i < content.length; i++) {
            System.out.print(content[i]);
            if (i < content.length -1) {
                System.out.print(", ");
            }
        }
        
        //New line..
        System.out.println();
        
        //The main game loop
        while (game) {
        
            //This is the input we will enter to the console.
            inputString = console.nextLine();
            
            //If we are not in any of the games..
            if (!spinGame && !tofGame) {
                //Looping through the array
                for (String s : content) {
                    //If the entered input, contains any word that is in the array, enter switch statement, to find out which word was it.
                    if (inputString.equalsIgnoreCase(s)) {
                        //Entering switch statement to find out which of the values matched in the array.
                        switch (inputString) {
                            //Spin game match
                            case "spin":
                                //Start spin game
                                spinGame = true;
                            break;
                            
                            //Tof game match.
                            case "tof":
                                //Start tof game
                                tofGame = true;                        
                            break;                            
                        }
                        //Getting out of the for loop, so we won't get could not find game error after match found.
                        break;
                    } else {
                        //No match found, throw error.
                        System.out.println("Could not find game!");
                        //Break out of the loop.
                        break;
                    }
                }                
            }
            
            /*
            * Spin Game Start
            */
            
            
            //Setting up the text once user entered the game.
            
            if (spinGame) {
                System.out.println("Welcome to the spin game!");
                System.out.println("Please enter 'start' to start and 'back' to go back.");
            }
            
            /*
            * Main Spin Game loop
            */
            
            while (spinGame) {
            
                //This is the input we will pass the console.
                inputString = console.nextLine();
                
                //Looping through the array for selected navigation option.
                for (String s : navigateGame) {
                    //Checking if the entered value contains any word inside the navgiation options array.
                    if (inputString.equalsIgnoreCase(s)) {
                        //Entering the switch statement to find out what word.
                        switch (inputString) {
                            
                            //Case start game
                            //This will spin the wheel once, and tell results.
                            case "start":
                                //Printing the spinned value frm spinWheel().
                                System.out.println(spin.spinWheel());
                                //Setting the console to receive more inputs.
                                inputString = console.nextLine();
                            break;
                            
                            //Case back
                            //This will go back to the lobby.
                            case "back":
                                //Quit spinGame.
                                spinGame = false;
                                //Back to the top of the method.
                                main(args);
                            break;
                        }
                    }
                }
            }
        }
    }
}

I did not make the spin part because I really need to know if I am doing well. Because I see most of the people are doing things in the different way, I hate sticking with a one way while I think it's incorrect.

Is there anything wrong with my way of coding and handling?

deleted 153 characters in body
Source Link
import java.util.Scanner;
import games.Spin;

public class Mains {
    //Declaring Scanner object as console.
    static Scanner console = new Scanner(System.in);
    //Declaring Spin game object as spin
    static Spin spin = new Spin();
    
    //Available games array
    static String[] content = {"spin", "tof"};
    //Navigation options array
    static String[] navigateGame = {"start", "back"};
    
    public static void main (String[] args) {
        
        // Setting the default number for inputNumber
        int inputNumber = 0;
        
        // Setting the defult value for inputString
        String inputString = "";
        
        // The main game loop
        boolean game = true;
        
        // Spin game loop
        boolean spinGame = false;
        
        // Truth or false game loop
        boolean tofGame = false;
        
        // Not found boolean, if a game wasn't found.
        // A good solution to not loop through all games when printing.
        boolean notFound = false;           
        
        System.out.print("Welcome! Please select a game: ");
        for (int i = 0; i < content.length; i++) {
            System.out.print(content[i]);
            if (i < content.length -1) {
                System.out.print(", ");
            }
        }
        
        //New line..
        System.out.println();
        
        //The main game loop
        while (game) {
        
            //This is the input we will enter to the console.
            inputString = console.nextLine();
            
            //If we are not in any of the games..
            if (!spinGame && !tofGame) {
                //Looping through the array
                for (String s : content) {
                    //If the entered input, contains any word that is in the array, enter switch statement, to find out which word was it.
                    if (inputString.equalsIgnoreCase(s)) {
                        //Entering switch statement to find out which of the values matched in the array.
                        switch (inputString) {
                            //Spin game match
                            case "spin":
                                //Start spin game
                                spinGame = true;
                            break;
                            
                            //Tof game match.
                            case "tof":
                                //Start tof game
                                tofGame = true;                     
                            break;                          
                        }
                        //Getting out of the for loop, so we won't get could not find game error after match found.
                        break;
                    } else {
                        //No match found, throw error.
                        System.out.println("Could not find game!");
                        //Break out of the loop.
                        break;
                    }
                }               
            }
            
            /*
            * Spin Game Start
            */
            
            
            //Setting up the text once user entered the game.
            
            if (spinGame) {
                System.out.println("Welcome to the spin game!");
                System.out.println("Please enter 'start' to start and 'back' to go back.");
            }
            
            /*
            * Main Spin Game loop
            */
            
            while (spinGame) {
            
                //This is the input we will pass the console.
                inputString = console.nextLine();
                
                //Looping through the array for selected navigation option.
                for (String s : navigateGame) {
                    //Checking if the entered value contains any word inside the navgiation options array.
                    if (inputString.equalsIgnoreCase(s)) {
                        //Entering the switch statement to find out what word.
                        switch (inputString) {
                            
                            //Case start game
                            //This will spin the wheel once, and tell results.
                            case "start":
                                //Printing the spinned value frm spinWheel().
                                System.out.println(spin.spinWheel());
                                //Setting the console to receive more inputs.
                                inputString = console.nextLine();
                            break;
                            
                            //Case back
                            //This will go back to the lobby.
                            case "back":
                                //Quit spinGame.
                                spinGame = false;
                                //Back to the top of the method.
                                main(args);
                            break;
                        }
                    }
                }
            }
        }
    }
}
import java.util.Scanner;
import games.Spin;

public class Mains {
    //Declaring Scanner object as console.
    static Scanner console = new Scanner(System.in);
    //Declaring Spin game object as spin
    static Spin spin = new Spin();
    
    //Available games array
    static String[] content = {"spin", "tof"};
    //Navigation options array
    static String[] navigateGame = {"start", "back"};
    
    public static void main (String[] args) {
        
        // Setting the default number for inputNumber
        int inputNumber = 0;
        
        // Setting the defult value for inputString
        String inputString = "";
        
        // The main game loop
        boolean game = true;
        
        // Spin game loop
        boolean spinGame = false;
        
        // Truth or false game loop
        boolean tofGame = false;
        
        // Not found boolean, if a game wasn't found.
        // A good solution to not loop through all games when printing.
        boolean notFound = false;           
        
        System.out.print("Welcome! Please select a game: ");
        for (int i = 0; i < content.length; i++) {
            System.out.print(content[i]);
            if (i < content.length -1) {
                System.out.print(", ");
            }
        }
        
        //New line..
        System.out.println();
        
        //The main game loop
        while (game) {
        
            //This is the input we will enter to the console.
            inputString = console.nextLine();
            
            //If we are not in any of the games..
            if (!spinGame && !tofGame) {
                //Looping through the array
                for (String s : content) {
                    //If the entered input, contains any word that is in the array, enter switch statement, to find out which word was it.
                    if (inputString.equalsIgnoreCase(s)) {
                        //Entering switch statement to find out which of the values matched in the array.
                        switch (inputString) {
                            //Spin game match
                            case "spin":
                                //Start spin game
                                spinGame = true;
                            break;
                            
                            //Tof game match.
                            case "tof":
                                //Start tof game
                                tofGame = true;                     
                            break;                          
                        }
                        //Getting out of the for loop, so we won't get could not find game error after match found.
                        break;
                    } else {
                        //No match found, throw error.
                        System.out.println("Could not find game!");
                        //Break out of the loop.
                        break;
                    }
                }               
            }
            
            /*
            * Spin Game Start
            */
            
            
            //Setting up the text once user entered the game.
            
            if (spinGame) {
                System.out.println("Welcome to the spin game!");
                System.out.println("Please enter 'start' to start and 'back' to go back.");
            }
            
            /*
            * Main Spin Game loop
            */
            
            while (spinGame) {
            
                //This is the input we will pass the console.
                inputString = console.nextLine();
                
                //Looping through the array for selected navigation option.
                for (String s : navigateGame) {
                    //Checking if the entered value contains any word inside the navgiation options array.
                    if (inputString.equalsIgnoreCase(s)) {
                        //Entering the switch statement to find out what word.
                        switch (inputString) {
                            
                            //Case start game
                            //This will spin the wheel once, and tell results.
                            case "start":
                                //Printing the spinned value frm spinWheel().
                                System.out.println(spin.spinWheel());
                                //Setting the console to receive more inputs.
                                inputString = console.nextLine();
                            break;
                            
                            //Case back
                            //This will go back to the lobby.
                            case "back":
                                //Quit spinGame.
                                spinGame = false;
                                //Back to the top of the method.
                                main(args);
                            break;
                        }
                    }
                }
            }
        }
    }
}
import java.util.Scanner;
import games.Spin;

public class Mains {
    //Declaring Scanner object as console.
    static Scanner console = new Scanner(System.in);
    //Declaring Spin game object as spin
    static Spin spin = new Spin();
    
    //Available games array
    static String[] content = {"spin", "tof"};
    //Navigation options array
    static String[] navigateGame = {"start", "back"};
    
    public static void main (String[] args) {
        
        // Setting the default number for inputNumber
        int inputNumber = 0;
        
        // Setting the defult value for inputString
        String inputString = "";
        
        // The main game loop
        boolean game = true;
        
        // Spin game loop
        boolean spinGame = false;
        
        // Truth or false game loop
        boolean tofGame = false;            
        
        System.out.print("Welcome! Please select a game: ");
        for (int i = 0; i < content.length; i++) {
            System.out.print(content[i]);
            if (i < content.length -1) {
                System.out.print(", ");
            }
        }
        
        //New line..
        System.out.println();
        
        //The main game loop
        while (game) {
        
            //This is the input we will enter to the console.
            inputString = console.nextLine();
            
            //If we are not in any of the games..
            if (!spinGame && !tofGame) {
                //Looping through the array
                for (String s : content) {
                    //If the entered input, contains any word that is in the array, enter switch statement, to find out which word was it.
                    if (inputString.equalsIgnoreCase(s)) {
                        //Entering switch statement to find out which of the values matched in the array.
                        switch (inputString) {
                            //Spin game match
                            case "spin":
                                //Start spin game
                                spinGame = true;
                            break;
                            
                            //Tof game match.
                            case "tof":
                                //Start tof game
                                tofGame = true;                     
                            break;                          
                        }
                        //Getting out of the for loop, so we won't get could not find game error after match found.
                        break;
                    } else {
                        //No match found, throw error.
                        System.out.println("Could not find game!");
                        //Break out of the loop.
                        break;
                    }
                }               
            }
            
            /*
            * Spin Game Start
            */
            
            
            //Setting up the text once user entered the game.
            
            if (spinGame) {
                System.out.println("Welcome to the spin game!");
                System.out.println("Please enter 'start' to start and 'back' to go back.");
            }
            
            /*
            * Main Spin Game loop
            */
            
            while (spinGame) {
            
                //This is the input we will pass the console.
                inputString = console.nextLine();
                
                //Looping through the array for selected navigation option.
                for (String s : navigateGame) {
                    //Checking if the entered value contains any word inside the navgiation options array.
                    if (inputString.equalsIgnoreCase(s)) {
                        //Entering the switch statement to find out what word.
                        switch (inputString) {
                            
                            //Case start game
                            //This will spin the wheel once, and tell results.
                            case "start":
                                //Printing the spinned value frm spinWheel().
                                System.out.println(spin.spinWheel());
                                //Setting the console to receive more inputs.
                                inputString = console.nextLine();
                            break;
                            
                            //Case back
                            //This will go back to the lobby.
                            case "back":
                                //Quit spinGame.
                                spinGame = false;
                                //Back to the top of the method.
                                main(args);
                            break;
                        }
                    }
                }
            }
        }
    }
}
Tweeted twitter.com/#!/StackCodeReview/status/352474271378976769
fixed code formatting...then SE made me fix something else :P
Source Link

I see many many programs, that are written out of the mainmain method String[] args. But most of my programs, are written in the main string argsmain, and accessingaccess some methods from other classes.

import java.util.Scanner; import games.Spin;

import java.util.Scanner;
import games.Spin;

public class Mains {
    //Declaring Scanner object as console.
    static Scanner console = new Scanner(System.in);
    //Declaring Spin game object as spin
    static Spin spin = new Spin();
    
    //Available games array
    static String[] content = {"spin", "tof"};
    //Navigation options array
    static String[] navigateGame = {"start", "back"};
    
    public static void main (String[] args) {
        
        // Setting the default number for inputNumber
        int inputNumber = 0;
        
        // Setting the defult value for inputString
        String inputString = "";
        
        // The main game loop
        boolean game = true;
        
        // Spin game loop
        boolean spinGame = false;
        
        // Truth or false game loop
        boolean tofGame = false;
        
        // Not found boolean, if a game wasn't found.
        // A good solution to not loop through all games when printing.
        boolean notFound = false;           
        
        System.out.print("Welcome! Please select a game: ");
        for (int i = 0; i < content.length; i++) {
            System.out.print(content[i]);
            if (i < content.length -1) {
                System.out.print(", ");
            }
        }
        
        //New line..
        System.out.println();
        
        //The main game loop
        while (game) {
        
            //This is the input we will enter to the console.
            inputString = console.nextLine();
            
            //If we are not in any of the games..
            if (!spinGame && !tofGame) {
                //Looping through the array
                for (String s : content) {
                    //If the entered input, contains any word that is in the array, enter switch statement, to find out which word was it.
                    if (inputString.equalsIgnoreCase(s)) {
                        //Entering switch statement to find out which of the values matched in the array.
                        switch (inputString) {
                            //Spin game match
                            case "spin":
                                //Start spin game
                                spinGame = true;
                            break;
                            
                            //Tof game match.
                            case "tof":
                                //Start tof game
                                tofGame = true;                     
                            break;                          
                        }
                        //Getting out of the for loop, so we won't get could not find game error after match found.
                        break;
                    } else {
                        //No match found, throw error.
                        System.out.println("Could not find game!");
                        //Break out of the loop.
                        break;
                    }
                }               
            }
            
            /*
            * Spin Game Start
            */
            
            
            //Setting up the text once user entered the game.
            
            if (spinGame) {
                System.out.println("Welcome to the spin game!");
                System.out.println("Please enter 'start' to start and 'back' to go back.");
            }
            
            /*
            * Main Spin Game loop
            */
            
            while (spinGame) {
            
                //This is the input we will pass the console.
                inputString = console.nextLine();
                
                //Looping through the array for selected navigation option.
                for (String s : navigateGame) {
                    //Checking if the entered value contains any word inside the navgiation options array.
                    if (inputString.equalsIgnoreCase(s)) {
                        //Entering the switch statement to find out what word.
                        switch (inputString) {
                            
                            //Case start game
                            //This will spin the wheel once, and tell results.
                            case "start":
                                //Printing the spinned value frm spinWheel().
                                System.out.println(spin.spinWheel());
                                //Setting the console to receive more inputs.
                                inputString = console.nextLine();
                            break;
                            
                            //Case back
                            //This will go back to the lobby.
                            case "back":
                                //Quit spinGame.
                                spinGame = false;
                                //Back to the top of the method.
                                main(args);
                            break;
                        }
                    }
                }
            }
        }
    }
}

package games;

package games;

public class Spin {
    
    public String spinWheel() {
        return "It works!";
    }
}

I see many many programs, that are written out of the main method String[] args. But most of my programs, are written in the main string args, and accessing some methods from other classes.

import java.util.Scanner; import games.Spin;

public class Mains {
    //Declaring Scanner object as console.
    static Scanner console = new Scanner(System.in);
    //Declaring Spin game object as spin
    static Spin spin = new Spin();
    
    //Available games array
    static String[] content = {"spin", "tof"};
    //Navigation options array
    static String[] navigateGame = {"start", "back"};
    
    public static void main (String[] args) {
        
        // Setting the default number for inputNumber
        int inputNumber = 0;
        
        // Setting the defult value for inputString
        String inputString = "";
        
        // The main game loop
        boolean game = true;
        
        // Spin game loop
        boolean spinGame = false;
        
        // Truth or false game loop
        boolean tofGame = false;
        
        // Not found boolean, if a game wasn't found.
        // A good solution to not loop through all games when printing.
        boolean notFound = false;           
        
        System.out.print("Welcome! Please select a game: ");
        for (int i = 0; i < content.length; i++) {
            System.out.print(content[i]);
            if (i < content.length -1) {
                System.out.print(", ");
            }
        }
        
        //New line..
        System.out.println();
        
        //The main game loop
        while (game) {
        
            //This is the input we will enter to the console.
            inputString = console.nextLine();
            
            //If we are not in any of the games..
            if (!spinGame && !tofGame) {
                //Looping through the array
                for (String s : content) {
                    //If the entered input, contains any word that is in the array, enter switch statement, to find out which word was it.
                    if (inputString.equalsIgnoreCase(s)) {
                        //Entering switch statement to find out which of the values matched in the array.
                        switch (inputString) {
                            //Spin game match
                            case "spin":
                                //Start spin game
                                spinGame = true;
                            break;
                            
                            //Tof game match.
                            case "tof":
                                //Start tof game
                                tofGame = true;                     
                            break;                          
                        }
                        //Getting out of the for loop, so we won't get could not find game error after match found.
                        break;
                    } else {
                        //No match found, throw error.
                        System.out.println("Could not find game!");
                        //Break out of the loop.
                        break;
                    }
                }               
            }
            
            /*
            * Spin Game Start
            */
            
            
            //Setting up the text once user entered the game.
            
            if (spinGame) {
                System.out.println("Welcome to the spin game!");
                System.out.println("Please enter 'start' to start and 'back' to go back.");
            }
            
            /*
            * Main Spin Game loop
            */
            
            while (spinGame) {
            
                //This is the input we will pass the console.
                inputString = console.nextLine();
                
                //Looping through the array for selected navigation option.
                for (String s : navigateGame) {
                    //Checking if the entered value contains any word inside the navgiation options array.
                    if (inputString.equalsIgnoreCase(s)) {
                        //Entering the switch statement to find out what word.
                        switch (inputString) {
                            
                            //Case start game
                            //This will spin the wheel once, and tell results.
                            case "start":
                                //Printing the spinned value frm spinWheel().
                                System.out.println(spin.spinWheel());
                                //Setting the console to receive more inputs.
                                inputString = console.nextLine();
                            break;
                            
                            //Case back
                            //This will go back to the lobby.
                            case "back":
                                //Quit spinGame.
                                spinGame = false;
                                //Back to the top of the method.
                                main(args);
                            break;
                        }
                    }
                }
            }
        }
    }
}

package games;

public class Spin {
    
    public String spinWheel() {
        return "It works!";
    }
}

I see many many programs that are written out of the main method. But most of my programs are written in main, and access some methods from other classes.

import java.util.Scanner;
import games.Spin;

public class Mains {
    //Declaring Scanner object as console.
    static Scanner console = new Scanner(System.in);
    //Declaring Spin game object as spin
    static Spin spin = new Spin();
    
    //Available games array
    static String[] content = {"spin", "tof"};
    //Navigation options array
    static String[] navigateGame = {"start", "back"};
    
    public static void main (String[] args) {
        
        // Setting the default number for inputNumber
        int inputNumber = 0;
        
        // Setting the defult value for inputString
        String inputString = "";
        
        // The main game loop
        boolean game = true;
        
        // Spin game loop
        boolean spinGame = false;
        
        // Truth or false game loop
        boolean tofGame = false;
        
        // Not found boolean, if a game wasn't found.
        // A good solution to not loop through all games when printing.
        boolean notFound = false;           
        
        System.out.print("Welcome! Please select a game: ");
        for (int i = 0; i < content.length; i++) {
            System.out.print(content[i]);
            if (i < content.length -1) {
                System.out.print(", ");
            }
        }
        
        //New line..
        System.out.println();
        
        //The main game loop
        while (game) {
        
            //This is the input we will enter to the console.
            inputString = console.nextLine();
            
            //If we are not in any of the games..
            if (!spinGame && !tofGame) {
                //Looping through the array
                for (String s : content) {
                    //If the entered input, contains any word that is in the array, enter switch statement, to find out which word was it.
                    if (inputString.equalsIgnoreCase(s)) {
                        //Entering switch statement to find out which of the values matched in the array.
                        switch (inputString) {
                            //Spin game match
                            case "spin":
                                //Start spin game
                                spinGame = true;
                            break;
                            
                            //Tof game match.
                            case "tof":
                                //Start tof game
                                tofGame = true;                     
                            break;                          
                        }
                        //Getting out of the for loop, so we won't get could not find game error after match found.
                        break;
                    } else {
                        //No match found, throw error.
                        System.out.println("Could not find game!");
                        //Break out of the loop.
                        break;
                    }
                }               
            }
            
            /*
            * Spin Game Start
            */
            
            
            //Setting up the text once user entered the game.
            
            if (spinGame) {
                System.out.println("Welcome to the spin game!");
                System.out.println("Please enter 'start' to start and 'back' to go back.");
            }
            
            /*
            * Main Spin Game loop
            */
            
            while (spinGame) {
            
                //This is the input we will pass the console.
                inputString = console.nextLine();
                
                //Looping through the array for selected navigation option.
                for (String s : navigateGame) {
                    //Checking if the entered value contains any word inside the navgiation options array.
                    if (inputString.equalsIgnoreCase(s)) {
                        //Entering the switch statement to find out what word.
                        switch (inputString) {
                            
                            //Case start game
                            //This will spin the wheel once, and tell results.
                            case "start":
                                //Printing the spinned value frm spinWheel().
                                System.out.println(spin.spinWheel());
                                //Setting the console to receive more inputs.
                                inputString = console.nextLine();
                            break;
                            
                            //Case back
                            //This will go back to the lobby.
                            case "back":
                                //Quit spinGame.
                                spinGame = false;
                                //Back to the top of the method.
                                main(args);
                            break;
                        }
                    }
                }
            }
        }
    }
}
package games;

public class Spin {
    
    public String spinWheel() {
        return "It works!";
    }
}
Source Link
Loading