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.
Am I programming in the correct way?
main.java:
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;
}
}
}
}
}
}
}
Spin.java:
package games;
public class Spin {
public String spinWheel() {
return "It works!";
}
}
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?