错误扫描字符串

时间:2015-08-14 21:45:58

标签: java console

我对编程非常陌生,今天我遇到了一个问题...我正在用控制台向高中制作一个Hangman游戏,但问题是,如果我尝试阅读opcionMenu,可以没有输入超过1个单词的电影,如果我手动更改opcionMenu的值,变量pelicula充电完美

感谢

游戏尚未完成

以下是代码:

package proyectoFinal;

import java.util.Scanner;

public class Proyecto {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    int opcionMenu = 10;
    String pelicula = "No hay pelicula";
    char letra;
    int espacios = 0;
    int vidas = 6, aciertos = 0;
    int cantidadPalabras, cantidadLetras;
    String intento;
    String mayuscula = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZ";
    String minuscula = mayuscula.toLowerCase();
    String jugador;


    System.out.println("Ingrese su nombre");
    jugador = input.nextLine ();

    while (opcionMenu != 0){
        System.out.println("...............................................\n"
                        + "\tOpcion \"1\" --- Ingresar Pelicula \n"
                        + "\tOpcion \"2\" --- Jugar \n"
                        + "\tOpcion \"0\" --- Salir \n"
                        + "\t\t DEBE SER UN NUMERO.\n"
                        + "...............................................");
        opcionMenu = input.nextInt(); //HERE'S THE PROBLEM AND IN THE NEXT 5 LINES


        switch (opcionMenu){
            case 1 :    System.out.println("Ingresar pelicula."); //Se ingresa la pelicula y la reconoce como mayusculas.

                        pelicula =input.next ().toUpperCase();      //SOLUCIONAR PARA QUE SE PUEDAN INGRESAR MAS DE 1 PALABRA

                        System.out.println("la pelicula es: " + pelicula + ".");
                        //System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
                        //              +  "\n\n\n\n\n\n\n\n\n");
                        //System.err.println("El nombre de la pelicula esta arriba ");

                        break;
            case 2 :    if(!"No hay pelicula".equals(pelicula)){
                            for (int i = 0; i < pelicula.length(); i++) {
                                letra = pelicula.charAt(i);
                                if(" ".equals(letra)){
                                    espacios++;

                                }

                            }
                            cantidadPalabras = espacios + 1;
                            cantidadLetras = pelicula.length() - espacios;

                            System.out.println("La pelicula tiene " + cantidadPalabras + " palabras.");
                            System.out.println("Hay " + cantidadLetras + " letras.");

                            //Arranca El JUEGO

                            System.out.println("=================AHORCADO================= VIDAS = " + vidas + " ACIERTOS = " + aciertos);
                            dibujar(vidas);

                            System.out.println("Ingrese una letra (MINUSCULA) o la palabra completa (MAYUSCULAS) si desea arriesgar :");

                            intento = input.next();

                            for(int i=0; i< mayuscula.length(); i ++) {
                                for (int e = 0; i < intento.length();  e++){// DA ERROR... SOLUCIONAR
                                    if (mayuscula.charAt(i) == intento.charAt(e)){
                                        if(intento.equals(pelicula)){

                                            dibujar(vidas);
                                            System.out.println("Correcto, la pelicula es " + pelicula.toUpperCase() + ".");
                                            System.out.println("\n ________________________________________");
                                            System.out.println(jugador + " WIN!!!");
                                        }



                                    }else{
                                        vidas = 0;
                                        dibujar(vidas);
                                        System.out.println("No es correcto. Haz perdido");
                                        System.out.println("la pelicula era " + pelicula);

                                        }


                                    }
                                } 


                    }else{
                            System.out.println("Se debe ingresar la pelicula antes de JUGAR.");
                        }
                        break;

            case 0 : break ;

            default: System.err.println("LA OPCION ELEGIDA NO ES CORRECTA");
        }

    }




    System.out.println("Se ha salido del juego");




}

private static void dibujar(int i) {
    switch (i) {
    case 6:
        System.out.println(" ---------------------");
        for (int j = 0; j < 15; j++) {
            System.out.println(" |");

        }
        System.out.println("__________");
        break;

    case 5:
        System.out.println(" ---------------------");
        System.out.println(" |                     |");
        System.out.println(" |                     |");
        System.out.println(" |                  -------");
        System.out.println(" |                 | -  -  |");
        System.out.println(" |                 |   o   |");
        System.out.println(" |                  -------");
        for (int j = 0; j < 10; j++) {
            System.out.println(" |");

        }
        System.out.println("__________");
        break;

    case 4:
        System.out.println(" ---------------------");
        System.out.println(" |                     |");
        System.out.println(" |                     |");
        System.out.println(" |                  -------");
        System.out.println(" |                 | -  -  |");
        System.out.println(" |                 |   o   |");
        System.out.println(" |                  -------");
        System.out.println(" |                     |   ");
        System.out.println(" |                     |   ");
        System.out.println(" |                     |   ");
        System.out.println(" |                     |   ");
        System.out.println(" |                     |   ");
        for (int j = 0; j < 5; j++) {
            System.out.println(" |");

        }
        System.out.println("__________");
        break;

    case 3:
        System.out.println(" ---------------------");
        System.out.println(" |                     |");
        System.out.println(" |                     |");
        System.out.println(" |                  -------");
        System.out.println(" |                 | -  -  |");
        System.out.println(" |                 |   o   |");
        System.out.println(" |                  -------");
        System.out.println(" |                     |   ");
        System.out.println(" |                   / |   ");
        System.out.println(" |                 /   |   ");
        System.out.println(" |                /    |   ");
        System.out.println(" |                     |   ");
        for (int j = 0; j < 5; j++) {
            System.out.println(" |");

        }
        System.out.println("__________");
        break;

    case 2:
        System.out.println(" ---------------------");
        System.out.println(" |                     |");
        System.out.println(" |                     |");
        System.out.println(" |                  -------");
        System.out.println(" |                 | -  -  |");
        System.out.println(" |                 |   o   |");
        System.out.println(" |                  -------");
        System.out.println(" |                     |   ");
        System.out.println(" |                   / | \\ ");
        System.out.println(" |                  /  |   \\ ");
        System.out.println(" |                 /   |     \\ ");
        System.out.println(" |                     |   ");
        for (int j = 0; j < 5; j++) {
            System.out.println(" |");

        }
        System.out.println("__________");
        break;

    case 1:
        System.out.println(" ---------------------");
        System.out.println(" |                     |");
        System.out.println(" |                     |");
        System.out.println(" |                  -------");
        System.out.println(" |                 | -  -  |");
        System.out.println(" |                 |   o   |");
        System.out.println(" |                  -------");
        System.out.println(" |                     |   ");
        System.out.println(" |                   / | \\ ");
        System.out.println(" |                  /  |   \\ ");
        System.out.println(" |                 /   |     \\ ");
        System.out.println(" |                     |   ");
        System.out.println(" |                    /  ");
        System.out.println(" |                   /      ");
        System.out.println(" |                  /       ");
        for (int j = 0; j < 2; j++) {
            System.out.println(" |");

        }
        System.out.println("__________");
        break;

    case 0:
       System.out.println(" ---------------------");
        System.out.println(" |                     |");
        System.out.println(" |                     |");
        System.out.println(" |                  -------");
        System.out.println(" |                 | X  X  |");
        System.out.println(" |                 |   o   |");
        System.out.println(" |                  -------");
        System.out.println(" |                     |   ");
        System.out.println(" |                   / | \\ ");
        System.out.println(" |                  /  |   \\ ");
        System.out.println(" |                 /   |     \\ ");
        System.out.println(" |                     |   ");
        System.out.println(" |                    / \\");
        System.out.println(" |                   /   \\  ");
        System.out.println(" |                  /     \\ ");
        for (int j = 0; j < 2; j++) {
            System.out.println(" |");

        }
        System.out.println("__________");
        System.out.println("GAME OVER");
        break;
    }
   }
    }

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您希望能够扫描包含多个单词的电影。如果是这样改变:

pelicula =input.next ().toUpperCase();

到:

pelicula = input.nextLine().toUpperCase();

因为input.next()只扫描第一个单词而忽略其余单词。