2014-09-25 84 views
0

所以我必須做一個程序類的遊戲,是一個猜數字遊戲,所以我做了所有的程序,但我想告訴用戶,你只有他有5個機會來猜測數字,如果用戶沒有在5個機會中猜出它打印一條消息,如「JAJAJAJA再試一次」,我不知道如何增加機會。有些東西是西班牙語,因爲即時通訊從西班牙,所以這就是爲什麼。 OHH該程序有一個菜單第一個選項是指令2是遊戲和3是退出即時通訊只是缺少增加機會的程序,如果用戶有一定的生活將是5.對不起,我的語法 奇科的意思就像很少或更低,格蘭德意味着大猜測一個機會的隨機數

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

class juego { 
    public static void main(String[] args) throws IOException { 
     proyecto obj = new proyecto(); 
     int w = (int) (5000 * Math.random() + 1); 
     String x; 
     int y; 
     do { 
      System.out.println("MENU"); 
      System.out.println("1.Instrucciones del juego"); 
      System.out.println("2. Juego"); 
      System.out.println("3. Salir"); 
      InputStreamReader isr = new InputStreamReader(System.in); 
      BufferedReader br = new BufferedReader(isr); 
      y = Integer.parseInt(br.readLine()); 
      switch (y) { 
      case 1: 
       obj.instu(); 
       break; 
      case 2: 
       obj.proce(w); 
       break; 
      case 3: 
       System.out.println("GRACIAS POR JUGAR"); 
       break; 
      default: 
       System.out.println("La opcion seleccionada no es valida"); 
      } 
     } while (y != 3); 
    } 
} 

class proyecto { 
    void instu() { 
     System.out.println("Instrucciones"); 
     System.out 
       .println("El juego se trata de adivinar el numero, tendras 10 intentos para poder   adivinar"); 
     System.out.println("Crees que podras ganar?"); 
    } 

    void proce(int w) throws IOException { 
     int e; 
     System.out.println("COMENCEMOS!!!!"); 
     InputStreamReader isr = new InputStreamReader(System.in); 
     BufferedReader br = new BufferedReader(isr); 
     System.out.println("INSERTE EL NUMERO"); 
     { 
      do { 
       e = Integer.parseInt(br.readLine()); 
       if (e < w) { 
        System.out.println("EL numero insertado es CHICO"); 
        System.out.println("Inserte un numero mas grande"); 
       } else { 
        System.out.println("EL numero insertado es muy grande"); 
       } 
       { 
        if (e == w) { 
         System.out.println("GANASTE"); 
        } 
       } 
      } while (e != w); 
     } 
    } 
} 
+0

Javascript在哪裏呢?或者確實是IOException? – 2014-09-25 05:59:14

+0

請添加堆棧跟蹤。 – Jens 2014-09-25 06:00:32

回答

1

(僅給予提示 - 假設這是功課,你一定要爲自己工作的細節。)

目前你的循環結構是:

do { 
    ... 
} while (e != w); 

所以你要循環直到玩家猜到了答案。你需要循環播放,直到玩家猜到回答完了。

如果你想保持你的電流回路結構,你可能想:

  • 保持一個變量(外循環聲明)用猜的次數至今
  • 遞增循環變量
  • 使用它在循環終止條件

或者,您可以將您的循環更改爲for循環:

for (int guess = 0; guess < 5; guess++) { 
    ... 
} 

...如果玩家得到正確的答案,就跳出循環。