Problema con l'applicazione che voglio creare

  • Risposte:0
Nicola Urgnani
  • Post del forum: 1

16 dic 2013, 14:23:03 Tramite pagina web

Buon giorno,
Sto creando questa applicazione: ci sono una serie di pulsanti (in questo momento solo due ma me ne serviranno 11) che cliccandoci sopra generano un numero random (dallo 0 al 6) e vorrei che man mano che si premono i pulsanti, mi mantenga il conteggio di quante volte è uscito un determinato numero.
Ho provato con una variabile Int (Righe 32 - 63 - 105) ma ogni volta che clicco l'altro pulsante questa variabile torna a 0 e non si incrementa.
Come posso fare?

1package com.NiUrSoftware.box;
2
3import java.util.Random;
4import com.NiUrSoftware.box.R;
5import android.os.Bundle;
6import android.app.Activity;
7import android.widget.Button;
8import android.widget.TextView;
9import android.view.View;
10import android.view.View.OnClickListener;
11
12
13public class MainActivity extends Activity {
14
15 @Override
16 protected void onCreate(Bundle savedInstanceState) {
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.activity_main);
19
20 //Generare l'istruzione per eseguire il random
21 final Random myRandom = new Random();
22
23
24 //Definizione nomi oggetti presenti nella schermata visualizzata (activity_main)
25 Button Premi1 = (Button)findViewById(R.id.button1);
26 final Button Premi2 = (Button)findViewById(R.id.button2);
27 final TextView Azione1 =(TextView)findViewById(R.id.textView1);
28 final TextView Azione2 =(TextView)findViewById(R.id.textView2);
29 final TextView Risultato = (TextView)findViewById(R.id.textView6);
30 final int Reti;
31
32
33 //Azione svolta premendo PULSANTE1
34 Premi1.setOnClickListener(new OnClickListener(){
35
36 //Genera numero e scrivi una parola PULSANTE 1
37
38 @Override
39 public void onClick(View v) {
40 // TODO Auto-generated method stub
41
42 if (myRandom.nextInt(6) >= 5) {
43 Azione1.setText(String.valueOf("CINQUE"));
44
45 }
46 else if (myRandom.nextInt(6) >= 4){
47 Azione1.setText(String.valueOf("QUATTRO"));
48
49 }
50 else if (myRandom.nextInt(6) >= 3){
51 Azione1.setText(String.valueOf("TRE"));
52
53 }
54 else if (myRandom.nextInt(6) >= 2){
55 Azione1.setText(String.valueOf("DUE"));
56
57
58 }
59 else if (myRandom.nextInt(6) >= 1){
60 Azione1.setText(String.valueOf("GOL"));
61 int Reti = 1;
62 Risultato.setText(String.valueOf(Reti));
63
64 }
65 else {
66 Azione1.setText(String.valueOf("ZERO"));
67
68 }
69
70
71 }});
72
73 //Azione svolta premendo PULSANTE2
74 Premi2.setOnClickListener(new OnClickListener(){
75
76 //Genera numero e scrivi una parola PULSANTE 2
77 @Override
78 public void onClick(View v) {
79 // TODO Auto-generated method stub
80
81 if (myRandom.nextInt(6) >= 5) {
82 Azione2.setText(String.valueOf("CINQUE"));
83
84
85 }
86 else if (myRandom.nextInt(6) >= 4){
87 Azione2.setText(String.valueOf("QUATTRO"));
88
89
90 }
91 else if (myRandom.nextInt(6) >= 3){
92 Azione2.setText(String.valueOf("TRE"));
93
94
95 }
96 else if (myRandom.nextInt(6) >= 2){
97 Azione2.setText(String.valueOf("DUE"));
98
99
100 }
101 else if (myRandom.nextInt(6) >= 1) {
102 Azione2.setText(String.valueOf("GOL"));
103 int Reti = Reti + 1;
104 Risultato.setText(String.valueOf(Reti));
105 }
106
107 else {
108 Azione2.setText(String.valueOf("ZERO"));
109
110 }
111 }});
112
113
114
115
116
117
118
119 }
120
121 }

— modificato il 16 dic 2013, 14:26:17

Rispondere