java - 扫描字符串时出错

标签 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;
    }
   }
    }

最佳答案

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

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

至:

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

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

关于java - 扫描字符串时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32019105/

相关文章:

java - 谁能推荐一系列不错的 Java 培训视频

logging - 如何将 Golang 的日志输出设置回控制台?

c# - 在 Visual Studio 中时 AllocConsole 不打印

java - 任务 ':app:mergeDebugResources'的执行失败。 > java.util.concurrent.ExecutionException:com.android.builder.internal.aapt.v2.Aapt2Exception:

java - 嵌套的 iText PdfPTable 边框宽度

java - 如何让paintComponent跟随鼠标移动?

java - 尝试从 Json Web 响应获取 Json 节点时出错

java - Log4j 如何阻止我的记录器打印到控制台?

javascript - 使用 var 关键字在 Javascript 中创建对象时遇到问题

wcf - IIS 托管应用程序中的控制台输出在哪里?