作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在自学 Java。其中一项作业是构建一个 GUI 程序,用户可以在其中移动 slider 并选择 100 到 1000 之间的数字。他们点击按钮提交该数字,然后两个骰子就会“滚动”多次。每个骰子的数字相加然后存储在一个数组中。掷骰子达到指定次数(基于 slider )后,打印一个直方图(使用 * 字符),显示每个数字掷出的总百分比。每个*代表总卷数的1%。因此,应该显示大约 100 个 *。
示例 session :- 人员将 slider 调整到所需位置。- 人按下按钮。- 模拟结果显示在文本区域中。TextArea 中可能显示的内容示例:掷骰子模拟结果每个“*”代表总卷数的1%。总卷数 = 1000。看起来像这样:
2: ***
3: ***
4: ***********
5: ***********
6: ********
7: ******************
8: ****************
9: **********
10: *************
11: *****
12: **
我将把我当前的代码放在下面。我真的很困惑如何在数组中存储未知数量的数字(可能是 100 到 1000 之间的任何数字)。我是否仍然构建数组以容纳最多 1000 个值?
这是我当前的代码:
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);
}
}
最佳答案
Java 为您提供了一种称为 ArrayList 的东西。ArrayList 支持可以根据需要增长的动态数组。
示例
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");
如果您想了解更多信息,请点击此链接 https://www.tutorialspoint.com/java/java_arraylist_class.htm
关于java - 在数组中存储未知数量的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46745603/
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Combination of List> 我有多个列表,可以是 2 个或 3 个,最多 10 个列表,有多个
我是一名优秀的程序员,十分优秀!