This question already has an answer here:
i am trying to make a game when one button is pressed it statrs the loop again and the the number and the color of the two button change but if pressed the wrong one the loop will break and send u to another activty i am stuck every thing work but the random number generator doesn't generate different random numberting
import java.util.Random;
public class game extends AppCompatActivity {
int clickcount = 0 ;
private Button a;
private Button b;
private Random l;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.action2);
a = (Button) findViewById(R.id.b1);
b = (Button) findViewById(R.id.b2);
while( clickcount > -1 ){
l = new Random();
final int p = (l.nextInt(1));
i want p to change every time the loop starts depending on what the user pressed
final String[] all = {"a", "b";
final int p = (int)(Math.floor(Math.random()* 8) + 1);
if (all[p].equals("a")) {
a.setBackgroundResource(R.drawable.btnchange);
b.setBackgroundResource(R.drawable.btncolor);
}
if (all[p].equals("b")) {
a.setBackgroundResource(R.drawable.btncolor);
b.setBackgroundResource(R.drawable.btnchange );
}
if (p == 0){
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clickcount = - 1;
}
});
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clickcount = clickcount + 1;
}
});
}
if (p == 1){
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clickcount = clickcount + 1;
}
});
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clickcount = - 1;
}
});
}
here i want it to start the loop again if the user pressed the right button
return; }
Intent i = new Intent(this, avctivtythree.class);
i.putExtra("score", clickcount);
startActivity(i);
finish();
}
}
Aucun commentaire:
Enregistrer un commentaire