Handling 20-face dice

This commit is contained in:
thibaud 2017-04-20 14:10:52 +02:00
parent 531ebd6646
commit e0ed947d30
2 changed files with 9 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
import java.util.StringTokenizer;
/**
* Created by thibaud on 20/04/17.
@ -15,14 +16,17 @@ import java.util.Random;
public class DiceActivity extends Activity implements View.OnClickListener {
private TextView textResult;
private int max;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dice);
max = getIntent().getIntExtra("max", 0);
TextView textTitle = (TextView)findViewById(R.id.textTitle);
textTitle.setText("6-sided dice");
textTitle.setText(String.valueOf(max) + "-sided dice");
textResult = (TextView)findViewById(R.id.textResult);
textResult.setText("");
@ -34,7 +38,7 @@ public class DiceActivity extends Activity implements View.OnClickListener {
@Override
public void onClick(View v){
Random rand = new Random();
int result = rand.nextInt(6) + 1;
this.textResult.setText(result);
int result = rand.nextInt(max) + 1;
this.textResult.setText(String.valueOf(result));
}
}

View File

@ -18,6 +18,7 @@ public class MainActivity extends AppCompatActivity {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, DiceActivity.class);
intent.putExtra("max", 6);
startActivity(intent);
}
});
@ -27,6 +28,7 @@ public class MainActivity extends AppCompatActivity {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, DiceActivity.class);
intent.putExtra("max", 20);
startActivity(intent);
}
});