Java 嵌套循环以创建砖墙
我是编程新手,这是我第一次在这个网站上发帖。我目前正在进行一项任务,该任务允许用户输入一个介于1和20之间的数字,然后根据用户在JTextField中输入的砖墙层数创建砖墙。我能够创建一行,但我并不真正理解嵌套语句,我在web上看到的所有示例都让我更加困惑,所以我想知道是否有人可以帮助我更好地理解循环的嵌套Java 嵌套循环以创建砖墙,java,nested-loops,Java,Nested Loops,我是编程新手,这是我第一次在这个网站上发帖。我目前正在进行一项任务,该任务允许用户输入一个介于1和20之间的数字,然后根据用户在JTextField中输入的砖墙层数创建砖墙。我能够创建一行,但我并不真正理解嵌套语句,我在web上看到的所有示例都让我更加困惑,所以我想知道是否有人可以帮助我更好地理解循环的嵌套 //Package List import java.awt.*; import java.applet.*; import javax.swing.*; import java.util.
//Package List
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class Wall extends JApplet implements KeyListener {
//Component declaration
JLabel directions;
JTextField input;
//Variable declaration
int startX = 50;
int startY = 650;
int width = 50;
int height = 20;
int spacing = 2;
//Method declaration
public void init()
{
getContentPane().setBackground(new Color (128, 128, 128));//Changes backround of JApplet to black
//Set JTextField and JLabel
setLayout (new FlowLayout( ));
directions = new JLabel("Enter in any number between 1 and 20 and then press Enter on your keyboard.");
input = new JTextField ( 10 );
add (directions );
add (input);
//Key listener
addKeyListener( this );
setFocusable( true );
}
//Method declaration
public void paint(Graphics g)
{
super.paint (g);
for (int col=1; col<= 8; col++)
{
// for (int row; row<=20; row++)
{ g.setColor (Color.RED);
g.fillRect (startX, startY, 50, 20);
startX = startX + spacing + width;
}
}
}
//Key event methods
public void keyReleased( KeyEvent ke ){}
public void keyTyped (KeyEvent ke ) {}
public void keyPressed ( KeyEvent ke )
{
int key = ke.getKeyCode ( );
if (key == KeyEvent.VK_ENTER)
{
}
}
//包列表
导入java.awt.*;
导入java.applet.*;
导入javax.swing.*;
导入java.util.*;
导入java.awt.event.*;
公共类Wall扩展JApplet实现KeyListener{
//组件声明
JLabel方向;
JTextField输入;
//变量声明
int startX=50;
int startY=650;
整数宽度=50;
整数高度=20;
int间距=2;
//方法声明
公共void init()
{
getContentPane().setBackground(新颜色(128、128、128));//将JApplet的背面改为黑色
//设置JTextField和JLabel
setLayout(新的FlowLayout());
方向=新的JLabel(“输入1到20之间的任意数字,然后按键盘上的Enter键”);
输入=新的JTextField(10);
添加(指示);
添加(输入);
//关键侦听器
addKeyListener(此);
设置聚焦(真);
}
//方法声明
公共空间涂料(图g)
{
超级油漆(g);
对于(int col=1;colint x=num//1-20
int step=num/8;
int last_row=num%8;
对于(int j=1;j,您需要根据当前的行/列/列计算砖的x
/
位置。现在,您只需在每个循环的开始处初始化这些值并根据需要递增,或者您可以使用一些数学
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class AnotherBrickInTheWall extends JApplet {
//Component declaration
JLabel directions;
JTextField input;
//Variable declaration
int startX = 0;
int startY = 50;
int width = 50;
int height = 20;
int spacing = 2;
//Method declaration
public void init() {
getContentPane().setBackground(new Color(128, 128, 128));//Changes backround of JApplet to black
//Set JTextField and JLabel
setLayout(new FlowLayout());
directions = new JLabel("Enter in any number between 1 and 20 and then press Enter on your keyboard.");
input = new JTextField(10);
add(directions);
add(input);
}
//Method declaration
@Override
public void paint(Graphics g) {
super.paint(g);
for (int row = 0; row < 8; row++) {
int y = startY + (row * (height + spacing));
for (int col = 0; col < 8; col++) {
int x = startX + (col * (width + spacing));
System.out.println(x + "x" + y);
g.setColor(Color.RED);
g.fillRect(x, y, width, height);
}
}
}
}
另外,JTextField
is有一个ActionListener
,当对字段进行操作时,通常由用户按Enter键触发。这意味着您不需要KeyListener
有关更多详细信息,请参阅。您需要相应地逐步调整x和y值……哇,你们太快了!谢谢你们的帮助!您能推荐一些关于编程的书籍吗?有很多书,其中一本比较常见和通用的可能是有效的Java,您可能会发现它很有用
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class AnotherBrickInTheWall extends JApplet {
//Component declaration
JLabel directions;
JTextField input;
//Variable declaration
int startX = 0;
int startY = 50;
int width = 50;
int height = 20;
int spacing = 2;
//Method declaration
public void init() {
getContentPane().setBackground(new Color(128, 128, 128));//Changes backround of JApplet to black
//Set JTextField and JLabel
setLayout(new FlowLayout());
directions = new JLabel("Enter in any number between 1 and 20 and then press Enter on your keyboard.");
input = new JTextField(10);
add(directions);
add(input);
}
//Method declaration
@Override
public void paint(Graphics g) {
super.paint(g);
for (int row = 0; row < 8; row++) {
int y = startY + (row * (height + spacing));
for (int col = 0; col < 8; col++) {
int x = startX + (col * (width + spacing));
System.out.println(x + "x" + y);
g.setColor(Color.RED);
g.fillRect(x, y, width, height);
}
}
}
}
int xOffset = 0;
for (int row = 0; row < 8; row++) {
int y = startY + (row * (height + spacing));
if (row % 2 == 0) {
xOffset = width / 2;
} else {
xOffset = 0;
}
for (int col = 0; col < 8; col++) {
int x = xOffset + (startX + (col * (width + spacing)));
System.out.println(x + "x" + y);
g.setColor(Color.RED);
g.fillRect(x, y, width, height);
}
}