From e0ed947d30381509016c10cc0bf673d78b10dad9 Mon Sep 17 00:00:00 2001 From: thibaud Date: Thu, 20 Apr 2017 14:10:52 +0200 Subject: [PATCH] Handling 20-face dice --- .../main/java/com/example/diceroller/DiceActivity.java | 10 +++++++--- .../main/java/com/example/diceroller/MainActivity.java | 2 ++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/example/diceroller/DiceActivity.java b/app/src/main/java/com/example/diceroller/DiceActivity.java index 894fcb5..312404b 100644 --- a/app/src/main/java/com/example/diceroller/DiceActivity.java +++ b/app/src/main/java/com/example/diceroller/DiceActivity.java @@ -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)); } } diff --git a/app/src/main/java/com/example/diceroller/MainActivity.java b/app/src/main/java/com/example/diceroller/MainActivity.java index 98dea24..9c9b7cc 100644 --- a/app/src/main/java/com/example/diceroller/MainActivity.java +++ b/app/src/main/java/com/example/diceroller/MainActivity.java @@ -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); } });