limite contador
Publicado por Charly (69 intervenciones) el 21/03/2017 21:12:15
Hola, estoy creando una app para el juego del número secreto.
Quiero que se puedan realizar 20 intentos, pero al llegar al último se congela la app.
Este es el código:
Antes tenía el if fuera del do y lo metí para probar, pero tampoco funciona.
Creo que el error lo tengo en el tope que le pongo al contador.
Quiero que se puedan realizar 20 intentos, pero al llegar al último se congela la app.
Este es el código:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package charly.android.pc.numerosecreto;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;
public class NumerosecretoActivity extends AppCompatActivity{
TextView txtIntro;
EditText txtNum;
Button btnProbar;
Button btnJueNue;
TextView resultado;
int numale;
int cont;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numerosecreto);
txtIntro=(TextView)findViewById(R.id.txtIntro);
txtNum=(EditText)findViewById(R.id.txtNum);
btnProbar=(Button)findViewById(R.id.btnProbar);
btnJueNue=(Button)findViewById(R.id.btnJueNue);
resultado=(TextView)findViewById(R.id.resultado);
txtNum.setEnabled(false);
btnProbar.setEnabled(false);
Random r=new Random();
numale=r.nextInt(100)+1;
cont=0;
}
public void btnJueNue(View view){
txtNum.setEnabled(true);
btnProbar.setEnabled(true);
btnJueNue.setEnabled(false);
}
public void btnProbar(View view){
int num=Integer.parseInt(txtNum.getText().toString());
cont++;
do{
if(num<numale){
resultado.setText("Número mayor! - "+cont+" intentos!");
}else if(num>numale){
resultado.setText("Número menor! - "+cont+" intentos!");
}else if((num==numale)){
resultado.setText("Has acertado! - "+cont+" intentos!");
txtNum.setEnabled(false);
btnProbar.setEnabled(false);
btnJueNue.setEnabled(true);
}
if(cont==21){
resultado.setText("20 intentos agotados!");
txtNum.setEnabled(true);
btnProbar.setEnabled(true);
btnJueNue.setEnabled(false);
}
}while(cont==21);
}
}
Creo que el error lo tengo en el tope que le pongo al contador.
Valora esta pregunta


0