Java “创建多个对象”;x";距离
我正试图完成一项作业的代码,但最后一部分我被卡住了。我需要在“天空”中创建“星星”(小的黄色方形对象)。。。由5行10颗星组成的网格。我是一名java初学者,应该使用star.moveHorizontal()或star.moveVertical()等方法。我搜索的所有相关帖子都太复杂或超出了我的理解范围 我想我需要创建一个数组?我们甚至还没有在课堂上讨论过。。。然后让每个“星”的距离为x(第一颗星)+右侧30个单位。然后,在连续出现10颗星之前,不要继续这种趋势。然后再得到四排10颗星 以下是我为一颗星创建的代码(在我的窗口左上角): 然后我尝试为一个正方形类创建一个数组。。。我真的不知道这是否合法Java “创建多个对象”;x";距离,java,Java,我正试图完成一项作业的代码,但最后一部分我被卡住了。我需要在“天空”中创建“星星”(小的黄色方形对象)。。。由5行10颗星组成的网格。我是一名java初学者,应该使用star.moveHorizontal()或star.moveVertical()等方法。我搜索的所有相关帖子都太复杂或超出了我的理解范围 我想我需要创建一个数组?我们甚至还没有在课堂上讨论过。。。然后让每个“星”的距离为x(第一颗星)+右侧30个单位。然后,在连续出现10颗星之前,不要继续这种趋势。然后再得到四排10颗星 以下是我
Square[] starArray = new Square[10];
for ( int i=0; i<starArray.length; i++) {
starArray[i] = new Square();
Square[]starArray=newsquare[10];
对于(int i=0;i如果你可以让一颗星星出现,但还没有学会数组,我认为这不是你老师想要的答案。数组的意义在于成为一个容器,这样你就可以再次引用对象。如果你以后不需要再回到星星,只需要创建它们并在循环中设置它们的值
// Set defaults for spacing and start positions
int horizontalStartPosition = 10;
int horizontalSpacing = 30;
int verticalStartPosition = 10;
int verticalSpacing = 30;
// Outer loop creates the 4 rows
for (int i = 0; i < 4; i++) {
// Inner loop creates each row
for (int j = 0; j < 10; j++) {
// Create the next star in the row
Square s = new Square();
s.makeVisible();
s.changeColor("yellow");
s.changeSize(5);
// Move the star to the correct vertical position for the current row
s.moveVertical(verticalStartPosition + i * verticalSpacing);
// Move the star to the correct horizontal spacing for the next star
s.moveHorizontal(horizontalStartPosition + j * horizontalSpacing);
}
}
//设置间距和起始位置的默认值
int水平起始位置=10;
int水平间距=30;
int垂直起始位置=10;
int垂直间距=30;
//外部循环创建4行
对于(int i=0;i<4;i++){
//内部循环创建每一行
对于(int j=0;j<10;j++){
//创建行中的下一颗星
正方形s=新正方形();
s、 使可见();
s、 变色(“黄色”);
s、 改变大小(5);
//将星形移动到当前行的正确垂直位置
s、 垂直移动(垂直起始位置+i*垂直间距);
//将星形移动到下一个星形的正确水平间距
s、 水平移动(水平起始位置+j*水平间距);
}
}
根据我的理解,您可能希望调用一个方法,该方法基本上可以为您在网格中放置一个星号。
我不完全明白你的意思,但以下是我能提供给你的:
您需要创建一个放置星形的方法
private static int X_SPACING = 30;
private static int Y_SPACING = 20;
public Square placeStar(int x, int y){
// This is the same code that you had before.
Square sq = new Square();
sq.changeColor("yellow");
sq.changeSize(5);
sq.moveVertical(-y); // Where Y is the distance from the TOP LEFT.
sq.moveHorizontal(-x);
return sq;
}
public ArrayList<Square> makeRow(int columnsAmount, int y, int startX){
ArrayList<Square> squares = new ArrayList<>();
for(int i = 0; i < columnsAmount; i++)
squares.add(placeStar(startX + (X_SPACING * i), y));
return squares;
}
public ArrayList<Square> makeGrid(int rowsAmount, int columnsAmount){
ArrayList<Square> rslt = new ArrayList<>();
for(int i = 0; i < rowsAmount; i++)
rslt.addAll(makeRow(columnsAmount, (i * Y_SPACING), 0);
return rslt;
}
专用静态int X_间距=30;
专用静态输入Y_间距=20;
公共广场placeStar(整数x,整数y){
//这与您以前使用的代码相同。
Square sq=新的Square();
sq.changeColor(“黄色”);
平方米(5);
sq.moveVertical(-y);//其中y是距左上角的距离。
水平移动平方(-x);
返回sq;
}
公共阵列列表生成行(int columnsAmount、int y、int startX){
ArrayList squares=新的ArrayList();
for(int i=0;i
基本上,调用“makeRow”应该创建一行[rowsAmount]星,它们都由[X_间距]像素分隔。
然后,makeGrid将多次调用makeRow调整[y],这将使您成为一个网格。请随意调整代码中的任何值
编辑:我添加了一个makeGrid函数,并在输入错误时更改了makeRow中的一些变量。我让这些函数返回一个ArrayList,但你不应该需要它们,除非你的老师稍后要求修改它们,或者其他什么
我希望这有帮助,不要犹豫,问更多的问题。
Sneling。您的思路正确。您可以使用5行10列的2D数组。请尝试以下操作:
int numColumns = 10; //the number of columns in the array
int numRows = 5; // the number of rows in the array
Square[][] starArray = new Square[numRows][numColumns]; // the array itself
final int DIST_X = 10; //the distance between columns (use final because this value should not change)
final int DIST_Y = 10; // the distance between rows (use final because this value should not change)
int y = 0; // the initial row's vertical displacement
for ( int i=0; i<numRows; i++) {
int x = 0; // the initial columns horizontal displacement
for ( int j=0; j<numColumns; j++) {
starArray[i][j] = new Square(); //define the square
starArray[i][j].moveHorizontal(x); // move it x units horizontally
starArray[i][j].moveVertical(y); // move it y units vertically
starArray[i][j].makeVisible(); //show the square
x += DIST_X; //update your horizontal displacement so the next column shows up in the correct position
}
y += DIST_Y; //update your vertical displacement so the next row shows up in the correct position
}
int numColumns=10;//数组中的列数
int numRows=5;//数组中的行数
Square[]starArray=new Square[numRows][numColumns];//数组本身
final int DIST_X=10;//列之间的距离(使用final,因为此值不应更改)
final int DIST_Y=10;//行之间的距离(使用final,因为此值不应更改)
int y=0;//初始行的垂直位移
对于(int i=0;i)您必须在何处显示开始?在简单控制台或Swing中,无需创建数组,除非您需要在稍后阶段引用正方形。我认为最好放置s.makeVisible()
结尾。您还需要将s
添加到其容器中。@c0der我也考虑过这样做,但由于OP没有共享任何其他代码,因此我认为最好不要更改其显示星号的代码。我没有将s
添加到容器中,因为在阅读时,这不在问题的范围之内。
int numColumns = 10; //the number of columns in the array
int numRows = 5; // the number of rows in the array
Square[][] starArray = new Square[numRows][numColumns]; // the array itself
final int DIST_X = 10; //the distance between columns (use final because this value should not change)
final int DIST_Y = 10; // the distance between rows (use final because this value should not change)
int y = 0; // the initial row's vertical displacement
for ( int i=0; i<numRows; i++) {
int x = 0; // the initial columns horizontal displacement
for ( int j=0; j<numColumns; j++) {
starArray[i][j] = new Square(); //define the square
starArray[i][j].moveHorizontal(x); // move it x units horizontally
starArray[i][j].moveVertical(y); // move it y units vertically
starArray[i][j].makeVisible(); //show the square
x += DIST_X; //update your horizontal displacement so the next column shows up in the correct position
}
y += DIST_Y; //update your vertical displacement so the next row shows up in the correct position
}