嵌套for循环java
我有一个家庭作业项目,我一直在做。或者只是其中的一部分,需要帮助解决。我基本上是用java来创建随机大小的建筑,在这些建筑中我会创建随机窗口。到目前为止,我随便找了几栋楼,但每栋楼只有一扇窗户可以弹出。当我尝试嵌套for循环时,它会将窗口移动到屏幕的末尾。我觉得问题在x坐标上,需要以某种方式重置它。任何帮助都将不胜感激。该程序分为三个部分:建筑类、面板和主体。 这是我编程的第二学期,所以我真的很新 顺便说一句,我已经把它交上来并获得了学分,但我仍然很想知道如何完成这部分课程嵌套for循环java,java,user-interface,loops,for-loop,Java,User Interface,Loops,For Loop,我有一个家庭作业项目,我一直在做。或者只是其中的一部分,需要帮助解决。我基本上是用java来创建随机大小的建筑,在这些建筑中我会创建随机窗口。到目前为止,我随便找了几栋楼,但每栋楼只有一扇窗户可以弹出。当我尝试嵌套for循环时,它会将窗口移动到屏幕的末尾。我觉得问题在x坐标上,需要以某种方式重置它。任何帮助都将不胜感激。该程序分为三个部分:建筑类、面板和主体。 这是我编程的第二学期,所以我真的很新 顺便说一句,我已经把它交上来并获得了学分,但我仍然很想知道如何完成这部分课程 /**Written
/**Written by Paul Ancajima
* last updated 2/25/15
* RandomSkyline panel
* this is where i bring over Building and have them draw itself
*/
import javax.swing.*;
import java.awt.*;
public class SkylinePanel extends JPanel{
//initialize variables
private int x=5+(int)(Math.random()*((10-5))+1);
private int w;
private int l=120+(int)(Math.random()*((300-120)+1));
private int x1;
private int y1=150+(int)(Math.random()*((170-150))+1);
private int gap = 2+(int)(Math.random()*5-2);
//first windowX and windowY start at 0,0
private int windowX=5+(int)(Math.random()*((15-5))+1);
private int windowY = (y1)+(int)(Math.random()*((130-y1))+1);
private int starX=25+(int)(Math.random()*((50-25))+1);
private int roadX;
private int roadX2=10;
//random number of windows
private int winNum = (int)(Math.random()*5*x);
//arrays or buildings, windows,and stars;
//figure out how to use winNum
private Building[]windowArr= new Building[x];
private Star[]starArr = new Star[starX];
private Building[]buildingArr = new Building[x];
public SkylinePanel(){
//loop to create all the buildings
for(int y=0; y<buildingArr.length; y++){
buildingArr[y] = new Building(x1, y1, w, l, x, Color.blue);
x1=w+gap+x1;
y1=(int)(Math.random()*20)+120;
w=20+(int)+(Math.random()*((35-20))+1);
l=(int)(Math.random()*300)+80;
gap = 2+(int)(Math.random()*5-2);
//FIX THE LOOP
//for(int zz=0; zz<buildingArr.length; zz++){
windowArr[y]= new Building(windowX,windowY, 3, 3, x, Color.yellow);
windowX = x1+(int)(Math.random()*(((x1+(w-gap))-x1))+1);
windowY = 160+(int)(Math.random()*((y1-(160)))+1);
// }
for(int zz=0; zz<starArr.length; zz++){
starArr[zz]= new Star((int)(Math.random()*x1), 0+(int)(Math.random()*((100+0))+1), 1, 1,Color.yellow);
}
//the Dimension of the screen will be the size of width of all buildings plus all gaps = x1+w+gap
setPreferredSize (new Dimension(x1, 200));
setBackground (Color.black);
}
}
public void paintComponent (Graphics page){
super.paintComponent(page);
page.setColor(Color.black);
for(int i = 0; i<starArr.length;i++){
starArr[i].draw(page);
}
//loop to draw all the buildings
for(int i=0; i<buildingArr.length; i++){
buildingArr[i].draw(page);
}
//loop to draw all windows(needs work/ change to windowsArr.length)
for(int i=0; i<buildingArr.length; i++){
windowArr[i].draw(page);
}
page.setColor(Color.white);
page.fillOval(x1-35, 30, 30, 30);
page.setColor(Color.gray);
page.fillRect(0, 180, x1, 20);
page.setColor(Color.yellow);
for(int j=0; j<180; j++){
page.drawLine(roadX, 190, roadX2, 190);
roadX=roadX+20;
roadX2=roadX2+20;
}
}
}
/**Written by Paul Ancajima
* Building class
* last updated 2/25/15
*/
import java.awt.*;
public class Building{
private int x,y, width, height,window; private Color color;
public Building(int x1, int y1, int w, int h, int window, Color shade){
x= x1;
y=y1;
width =w;
height = h;
color = shade;
}
public int setX(int x1){
return x1;
}
public int setY(int y1){
return y1;
}
public int getWidth(int w){
return w;
}
public int getHeight(int h){
return h;
}
public void draw(Graphics page){
page.setColor(color);
page.fillRect(x, y, width, height);
}
public void getWindows(int x,int y,int w,int h,Color shade){
this.x=x;
this.y=y;
width=w;
height=h;
color=shade;
}
}
/**written by Paul Ancajima
* Skyline main
* last updated 2/25/15
*/
import javax.swing.*;
import java.awt.*;
public class RandomSkyline{
public static void main (String[] args){
JFrame frame = new JFrame ("Paul's city");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SkylinePanel());
frame.pack();
frame.setVisible(true);
}
}
保罗·安卡吉马写的/**
*最后更新日期:2015年2月25日
*天际线面板
*这就是我把建筑带过来,让它们自己画出来的地方
*/
导入javax.swing.*;
导入java.awt.*;
公共类skylenepanel扩展了JPanel{
//初始化变量
私有整数x=5+(整数)(Math.random()*((10-5))+1);
私人int w;
私有int l=120+(int)(Math.random()*((300-120)+1));
私有int-x1;
私有inty1=150+(int)(Math.random()*((170-150))+1;
私有整数间隔=2+(整数)(Math.random()*5-2);
//第一个windowX和windowY从0,0开始
私有intwindowx=5+(int)(Math.random()*((15-5))+1;
私有int窗口y=(y1)+(int)(Math.random()*((130-y1))+1);
私有int starX=25+(int)(Math.random()*((50-25))+1;
私人int roadX;
私人道路X2=10;
//随机窗口数
private int winNum=(int)(Math.random()*5*x);
//阵列或建筑物、窗户和星星;
//了解如何使用winNum
私人建筑[]windowArr=新建筑[x];
私人明星[]starArr=新星[starX];
私人建筑[]Buildingar=新建筑[x];
公共图书馆(NEPANEL){
//循环以创建所有建筑
对于(int y=0;y您的
建筑类需要包含一个窗口数组,如果您希望每个建筑有多个窗口。即
public class Building{
private int x,y, width, height,window; private Color color;
private Window[] windows = new Window[]; // This could also be done with a collection rather than an array, they are easier to work with if you are adding and removing.
public Building(int x1, int y1, int w, int h, int window, Color shade){
x= x1;
y=y1;
width =w;
height = h;
color = shade;
}
...
// Change getWindows to the following
public Window[] getWindows(){
return windows;
}
// add a method to generate the windows for the building.
public void createWindows(int numWindows) {
// initialise Window array based on parameter
windows = new Window[numWindows];
// loop through for the length of the array
for (int i = 0; i < windows.size; i++) {
windows[i] = new Window(); // insert whatever parameters you want for the window. You probably need to check they stay inside the bounds of the building at least.
}
}
公共类建筑{
私有int x,y,宽度,高度,窗口;私有颜色;
private Window[]windows=new Window[];//这也可以通过集合而不是数组来完成,如果要添加和删除集合,它们更容易使用。
公共建筑(内部x1、内部y1、内部w、内部h、内部窗户、彩色阴影){
x=x1;
y=y1;
宽度=w;
高度=h;
颜色=阴影;
}
...
//将getWindows更改为以下内容
公共窗口[]获取窗口(){
返回窗口;
}
//添加方法以生成建筑的窗。
公共void createWindows(int numWindows){
//基于参数初始化窗口数组
windows=新窗口[numWindows];
//循环查找数组的长度
对于(int i=0;i
你有一个建筑类型的窗户阵列?我看你的设计很糟糕。建筑
不应该有一组可以根据建筑位置定位的窗户吗?那太棒了,但我不知道怎么做。谢谢我现在正在做。看起来我必须提供一个窗口[]windows=new Window[]中的维度或数字。其中windows[i]=new Window()的部分表示构造函数窗口()不可见不确定这意味着什么请确保在窗口类中构造函数窗口()定义为public-->public Window(){..}没有窗口类。我想我应该使用一个方法从building类创建窗口。对不起,我很难解释我不完全理解的事情。