Está bien, sintaxis limpia y clara. Lo que te puedo recomendar es que lo hagas mediante un Thread y algo muy importante:
Aprovecha la POO, no hagas todo en el main. Así tendrás un código limpio, y fácil de mantener.
He modificado tu método para jugar de nuevo. Para que lee devuelva true o false a la condición del while del thread. Si se elige un caracter que no sea X, devuelve true a la variable de condición y se seguirá jugando. De lo contrario, saldrá del juego.
Prueba el code y me comentas. Un saludo.
Aprovecha la POO, no hagas todo en el main. Así tendrás un código limpio, y fácil de mantener.
Código (=java) [Seleccionar]
import java.util.Random;
import java.util.Scanner;
public class Loteria {
int choice;
int[] tickets;
int[] yourTickets;
int matched;
Scanner keyboard = new Scanner(System.in);
public Loteria(){
Jugar instance = new Jugar();
instance.start();
}
void generar_tickets(int eleccion){
System.out.println("\nPrinting tickets...");
System.out.println("----------------------");
Random generateNumbers = new Random();
tickets = new int[59];
yourTickets = new int[eleccion];
int i;
// Generate list of numbers
for(i = 0; i < tickets.length; i++) {
tickets[i] = generateNumbers.nextInt(60);
}
// Give him his tickets
System.out.print("\nThese are your tickets: \t");
for (i = 0; i < yourTickets.length; i++) {
yourTickets[i] = generateNumbers.nextInt(60);
System.out.print(yourTickets[i] + " ");
}
} // fin metodo
void comparar(){
System.out.println("\n\nLet's if you won something... \n");
// Check if something matched
for(int i = 0; i < yourTickets.length; i++) {
for(int j = 0; j < tickets.length; j++) {
if (yourTickets[i] == tickets[j]) {
matched++;
}
}
}
// Make a pause
try {
Thread.sleep(3000);
} catch(InterruptedException e) {
}
switch (matched) {
case 1:
System.out.println("You won $100!");
break;
case 2:
System.out.println("You won $200!");
break;
case 3:
System.out.println("You won $500!");
break;
case 4:
System.out.println("You won $800!");
break;
case 5:
System.out.println("GREAT. You've got the pot: $1000!");
break;
case 0:
System.out.println("Have luck next time!");
break;
default:
System.out.println("Something went wrong");
System.exit(1);
}
} // fin metodo
boolean jugar_denuevo() {
// Ask if he wants to try again
System.out.print("Would you like to try again? (X to exit) \t");
String tryAgain = keyboard.next();
if(tryAgain.compareToIgnoreCase("x") == 0) {
System.out.println("Thank you. Bye");
System.exit(0);
return false;
}
else {
// Just clear screen
for (int l = 0; l < 1000; l++) {
System.out.println();
}
return true;
}
}// fin metodo
// clase que empieza el juego mediante thread
private class Jugar extends Thread{
private boolean continuar=true; //condicion del thread
public void run() { // incia el thread
while(continuar) { // hace la tarea mientras continuar sea true
try {
System.out.println("$$$$$$$ LOTTO $$$$$$$");
System.out.println("Hi, how many tickets would you like to buy?");
System.out.println(" 1) 1 Ticket");
System.out.println(" 2) 2 Tickets");
System.out.println(" 3) 3 Tickets");
System.out.println(" 4) 4 Tickets");
System.out.println(" 5) 5 Tickets");
System.out.println(" 0) In bankrupt - Nothing\n");
choice = keyboard.nextInt();
if (choice == 0) {
System.out.println("EXITING...");
System.exit(0);
}
generar_tickets(choice);
comparar();
continuar = jugar_denuevo();
}
catch(Exception e){
System.out.println("Ha ocurrido un error");
}
}
}
}
public static void main(String[] args) {
new Loteria();
}
} // fin.
He modificado tu método para jugar de nuevo. Para que lee devuelva true o false a la condición del while del thread. Si se elige un caracter que no sea X, devuelve true a la variable de condición y se seguirá jugando. De lo contrario, saldrá del juego.
Prueba el code y me comentas. Un saludo.