Reputation: 29
I am teaching myself Java. One of the assignments is to build a GUI program where the user can move a slider and select a number between 100 and 1000. They hit a button to submit that number and two die are "rolled" that many times. The numbers from each dice are added together and then stored in an array. Once the dice have been thrown the specified number of times (based on the slider), print a histogram (using the * character) that shows the total percentage each number was rolled. Each * will represent 1% of the total rolls. So, there should be approximately 100 *’s shown.
Sample session: - Person adjusts the slider to desired position. - Person pushes the button. - The simulation results are shown in the TextArea. Example of what might be shown in the TextArea: DICE ROLLING SIMULATION RESULTS Each "*" represents 1% of the total number of rolls. Total number of rolls = 1000. It looks like this:
2: ***
3: ***
4: ***********
5: ***********
6: ********
7: ******************
8: ****************
9: **********
10: *************
11: *****
12: **
I will put my current code below. I am really struggling with how to store an unknown amount of numbers in an array (could be anywhere from 100 to 1000). Do I still build the array to fit up to 1000 values?
Here is my current code:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.stage.Stage;
import javafx.event.*;
import javafx.scene.control.*;
import javafx.scene.input.*;
import java.util.Random;
public class ThackerAssignment6 extends Application {
//add the fxml controls and fxml event methods here
@FXML Slider mySlider;
@FXML Label myLabel;
@FXML Button myButton;
@FXML protected void mouseDragged(MouseEvent event){
double currentValue = mySlider.getValue();
int intValue = (int) currentValue;
myLabel.setText(intValue + "");
myButton.setValue(intValue);
}
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("ThackerAssignment6.fxml"));
loader.setController(this);
Parent root = loader.load();
Scene myScene = new Scene(root,700,700);
primaryStage.setScene(myScene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
/*Start
Move Slider. That number is inputted when *button* is clicked.
randOne and randTwo are initialized that many times. Each time
both numbers are added together and stored in a 2 row array. Row
1 is Roll Number. Row 2 is the sum of both die results. Then something
about printing it with stars?
*/
Random randomOne = new Random();
int randOne = randomOne.nextInt(6) + 1;
Random randomTwo = new Random();
int randTwo = randomTwo.nextInt(6) + 1;
//int[] dieRoll = new int[intValue];
//System.out.println(dieRoll);
}
}
Upvotes: 0
Views: 97
Reputation: 1473
Java has something for you called as ArrayList. ArrayList supports dynamic arrays that can grow as needed.
Example
ArrayList x = new ArrayList();
// add elements to the array list
x.add("C");
x.add("A");
x.add("E");
// Remove elements from the array list
x.remove("F");
If you want learn more, follow this link https://www.tutorialspoint.com/java/java_arraylist_class.htm
Upvotes: 1