Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0095815

Browse files
Update Ex14-29
1 parent c9a61b4 commit 0095815

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

‎ch_14/Exercise14_29.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ public void start(Stage primaryStage) {
2828
}
2929

3030
static class BeanMachinePane extends Pane {
31-
private final int triangleSize;
31+
private final int numberOfPins;
3232
private final int numberOfSlots;
3333
private Circle[] pins;
3434

3535
public BeanMachinePane(double width, double height, int numberOfSlots) {
3636
setWidth(width);
3737
setHeight(height);
3838
this.numberOfSlots = numberOfSlots;
39-
this.triangleSize = circleCount(numberOfSlots);
39+
this.numberOfPins = getNumberOfPins(numberOfSlots);
4040
paint();
4141
}
4242

@@ -49,7 +49,7 @@ private void paint() {
4949
paneHeight * 0.8);
5050

5151
double distance = (lowerLine.getEndX() - lowerLine.getStartX()) / numberOfSlots;
52-
pins = new Circle[triangleSize];
52+
pins = new Circle[numberOfPins];
5353
int index = 0;
5454
for (int i = 1; i < numberOfSlots; i++) {
5555
double x = lowerLine.getStartX() + (i * distance * 0.50) + distance / 2;
@@ -97,12 +97,18 @@ private void paint() {
9797
getChildren().addAll(outlineShape);
9898
}
9999

100-
private int circleCount(int slots) {
101-
int count = 0;
102-
while (slots-- > 1) {
103-
count += slots;
104-
}
105-
return count;
100+
/**
101+
* @param slots the number of slots in the bean machine
102+
* @return the number of pins in the bean machine
103+
*/
104+
int getNumberOfPins(int slots) {
105+
int numPins = 0;
106+
do {
107+
slots--;
108+
numPins += slots;
109+
} while (slots > 1);
110+
111+
return numPins;
106112
}
107113

108114
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /