2015-08-14 130 views
1

我對編程非常陌生,今天我遇到了一個問題......我正在用控制檯製作一個Hangman遊戲給高中,但問題是如果我嘗試讀取opcionMenu,不能進入電影超過100字,如果我手動更改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; 
    } 
    } 
    } 

回答

0

如果我理解正確,您希望能夠掃描包含多個單詞的電影。如果是這樣的改變:

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

到:

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

因爲input.next()將只掃描了第一個字而忽略其他。

+0

此解決方案是否適合您? – pcarranz