我正在自学Java。其中一项任务是构建一个 GUI 程序,用户可以在其中移动滑块并选择 100 到 1000 之间的数字。他们点击按钮提交该数字,然后两个骰子被“滚动”了很多次。每个骰子的数字相加,然后存储在一个数组中。一旦骰子被掷出指定的次数(基于滑块),打印一个直方图(使用 * 字符),显示每个数字被掷出的总百分比。每个 * 将代表总卷的 1%。因此,应该显示大约 100 个 *。
示例会话: - 人员将滑块调整到所需位置。- 人按下按钮。- 模拟结果显示在 TextArea 中。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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句