[dart mdoe] Fix code example to compile and run with modern Dart versions

This commit is contained in:
Parker Lougheed 2023-09-10 10:36:12 -05:00 committed by GitHub
parent ee6a1d201f
commit 53faa33ac6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 12 deletions

View File

@ -29,33 +29,33 @@
import 'dart:math' show Random;
void main() {
print(new Die(n: 12).roll());
print(Die(n: 12).roll());
}
// Define a class.
class Die {
// Define a class variable.
static Random shaker = new Random();
static final Random shaker = Random();
// Define instance variables.
int sides, value;
// Define a method using shorthand syntax.
String toString() => '$value';
final int sides;
int? lastRoll;
// Define a constructor.
Die({int n: 6}) {
if (4 <= n && n <= 20) {
sides = n;
} else {
Die({int n = 6}) : sides = n {
if (4 > n || n > 20) {
// Support for errors and exceptions.
throw new ArgumentError(/* */);
throw ArgumentError(/* */);
}
}
// Define a method using shorthand syntax.
@override
String toString() => '$lastRoll';
// Define an instance method.
int roll() {
return value = shaker.nextInt(sides) + 1;
return lastRoll = shaker.nextInt(sides) + 1;
}
}
</textarea>