How can I do a java program that chooses random words?

0

Hello, good greetings, I would like to ask you one thing they sent me to do a program that randomly picks up an option of the machine and a user option I was doing it with import java.util.Random; but I realized that this is only applicable with numbers and when I get to the comparison time I want to compare the word of the machine = wordMachine = word user I can not do it anymore I can not compare a int with a string

this is what I have

package juego;
import java.util.Random;
import java.util.Scanner;

public class juego {    
    public static void main(String[] args) {
        Scanner sc = new Scanner (System.in);
        Random ga = new Random(); 

menu ( sc,   ga);

    }

    static void ramdom ( Scanner sc, Random ga) {
        System.out.println("Escoje entre Piedra / Papel / Tijeras "); 
        String eleccion;
        eleccion = sc.nextLine();

        int numeroAleatorio = ga.nextInt(3);

        System.out.println(numeroAleatorio);

        int resultado;
        resultado = numeroAleatorio;

        switch (resultado) { 
        case 0: 
            System.out.println("Piedra");
            break;
        case 1: 
            System.out.println("Papel");
            break;
        case 2: 
            System.out.println("Tijeras");
            break;
        }

If I can not explain myself well basically when choosing a random number I want it to be a word

    
asked by IamEac 21.11.2018 в 21:00
source

2 answers

0

Take a look at this code. Greetings!

static void ramdom ( Scanner sc, Random ga) {
    // Creo un arreglo con las posibles opciones
    String[] opciones = new String[3];

    opciones[0] = "Piedra";
    opciones[1] = "Papel";
    opciones[2] = "Tijeras";
    // Creo un arreglo con las posibles opciones

    System.out.println("Escoje entre Piedra / Papel / Tijeras "); 
    String eleccion = sc.nextLine();

    int numeroAleatorio = ga.nextInt(3);

    /*El número generado servirá como índice*/
    String opcion = opciones[numeroAleatorio]
    System.out.println(opcion); // acá mostrará Piedra Pepel o Tijeras según el número generado
}
    
answered by 22.11.2018 в 17:11
0

has helped me to order the code and the idea I had in the end I stay something if package tests; import java.util.Random; import java.util.Scanner; public class test {

public static void main(String[]args) {
    Scanner sc = new Scanner (System.in);
    menu(sc);
}
static void menu(Scanner sc) {
    int eleccion; 
    System.out.println("Opciones :"
            + "\nOperar = 1"
            + "\nSalir = 2");
    eleccion = sc.nextInt();
    switch(eleccion) {
    case 1:
        values(sc);
        break;
    case 2:
        System.out.print("AAdios..!!!");
        break;
    default: {
        System.err.print("Signo no reconocido");
        menu(sc);
    }
}
}
    static void values (Scanner sc) {
        int piedra = 0, papel = 1, tijeras = 2, x = 0;
        int maquina = (int) (Math.random()* 3 + 0);
        int numUsuario;
        int opcionUsuario;
        System.out.println("Escoje una opcion : \n0 Piedra"
                + "\n1 Papel"
                + "\n2 Tijeras");
        numUsuario = sc.nextInt();
        sc.nextLine();



        if(maquina == 0) {
            System.out.println("La maquina ha escogido Piedra");
        }else if (maquina == 1) {
            System.out.println("La maquina ha escogido Papel");

        }else if (maquina == 2) {
            System.out.println("La maquina ha escogido Tijeras");

        }

        if(numUsuario == piedra) {
            System.out.println("Tu Has escogido Piedra");
            if (maquina == piedra) {
                System.out.println("Empate");
            }else if (maquina == papel){
                System.out.println("Pierdes");
            }else if(maquina == tijeras) {
                System.out.println("Ganas");
            }
        }else if(numUsuario == papel) {
            System.out.println("Has escogido Papel");

            if (maquina == piedra) {
                System.out.println("Ganas");
            }else if (maquina == papel){
                System.out.println("Empate");
            }else if(maquina == tijeras) {
                System.out.println("Pierdes");
            }
        }else  if (numUsuario == tijeras) {
            System.out.println("Has escogido Tijeras");

            if (maquina == piedra) {
                System.out.println("Pierdes");
            }else if (maquina == papel){
                System.out.println("Ganas");
            }else if(maquina == tijeras) {
                System.out.println("Empate");
            }

    }
    }
}
    
answered by 22.11.2018 в 22:15