Skip to content
Advertisement

How to combine two programs in one class

I have created two games and now I am working on menu It should run each game after user selected each of them

Menu Must look like this:

Hello! Type ‘letter’ or ‘number’ to choose game

Heres first game:

import java.util.Scanner;

public class GuessTheNumber {

 public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String playAgain = "";
        
        System.out.println("Guess a number between 1 and 100:");
            int theNumber = (int)(Math.random() * 100 + 1);
            int numberOfTries = 0;
         
            int guess = 0;
        
            while (guess != theNumber) 
                
            {
                
                guess = scan.nextInt();
                numberOfTries = numberOfTries + 1;
                if (guess < theNumber)
                    System.out.println(guess + " is too low. Try again.");
                else if (guess > theNumber)
                    System.out.println(guess + " is too high. Try again.");
                else {
                    System.out.println(guess + " is correct. You win!");
                    System.out.println("It only took you " + numberOfTries + " tries! Good work!");
                }
            }
            System.out.println("Would you like to play again (y/n)?");
            playAgain = scan.next();
         while (playAgain.equalsIgnoreCase("y"));
        System.out.println("Thank you for playing! Goodbye.");
        scan.close();
    }

}

Heres the second

  import java.util.Scanner;

   public class GuessTheLetter {

 public static void main(String[] args) {
 Scanner scan = new Scanner(System.in);     
 String playAgain = "";
 int numberOfTries = 0;
 

do {
            

  System.out.println("Guess the Letter");


  char randomLetter = (char) (Math.random() * 26 + 65); 
  char enteredLetter = 0; 
  while(true){
     
     enteredLetter = Character.toUpperCase(scan.next().charAt(0)); 
     numberOfTries = numberOfTries + 1;
    if(enteredLetter==randomLetter)
    {
        System.out.println("Correct Guess");
        System.out.println("The letter is:"+randomLetter);
        System.out.println("It only took you " + numberOfTries + " tries! Good work!");      
        break;
    }
    else if(enteredLetter>randomLetter) 
    {
        System.out.println("Incorrect Guess");
        System.out.println("The letter entered is too high");
    }

    else if(enteredLetter<randomLetter) 
    {
        System.out.println("Incorrect Guess");
        System.out.println("The letter entered is too low");
    } 

   }
   
   
        System.out.println("Would you like to play again (y/n)?");
        playAgain = scan.next();
 }   while (playAgain.equalsIgnoreCase("y"));
 System.out.println("Thank you for playing! Goodbye.");
 scan.close();
  }}

Must i use multiple classes, or put everything into 1, how can i combine theese two games in one?

Advertisement

Answer

You can put each game into one function and then just call them in the main of that class. You do not need 2 classes for this. You can also add a condition that would decide which function to call.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement