如何使用 for 循环简化此代码?
How to simplify this code with for loops?
对于我的学校项目,我们正在创建一个纸牌游戏,我有一些硬编码的代码块。我希望有人能够在我附加的代码中看到一个模式,并能够用 for 循环压缩它。
// set the pointers of the cards in the pyramid
card [0].setPtrs (card [1], card [2]);
card [1].setPtrs (card [3], card [4]);
card [2].setPtrs (card [4], card [5]);
card [3].setPtrs (card [6], card [7]);
card [4].setPtrs (card [7], card [8]);
card [5].setPtrs (card [8], card [9]);
card [6].setPtrs (card [10], card [11]);
card [7].setPtrs (card [11], card [12]);
card [8].setPtrs (card [12], card [13]);
card [9].setPtrs (card [13], card [14]);
card [10].setPtrs (card [15], card [16]);
card [11].setPtrs (card [16], card [17]);
card [12].setPtrs (card [17], card [18]);
card [13].setPtrs (card [18], card [19]);
card [14].setPtrs (card [19], card [20]);
card [15].setPtrs (card [21], card [22]);
card [16].setPtrs (card [22], card [23]);
card [17].setPtrs (card [23], card [24]);
card [18].setPtrs (card [24], card [25]);
card [19].setPtrs (card [25], card [26]);
card [20].setPtrs (card [26], card [27]);
// set card X coords
cardCentreX [0] = 800;
cardCentreX [1] = 800 - card [1].getWidth () / 2;
cardCentreX [2] = 800 + card [1].getWidth () / 2;
cardCentreX [3] = cardCentreX [1] - card [1].getWidth () / 2;
cardCentreX [4] = cardCentreX [2] - card [1].getWidth () / 2;
cardCentreX [5] = cardCentreX [2] + card [1].getWidth () / 2;
cardCentreX [6] = cardCentreX [3] - card [1].getWidth () / 2;
cardCentreX [7] = cardCentreX [4] - card [1].getWidth () / 2;
cardCentreX [8] = cardCentreX [5] - card [1].getWidth () / 2;
cardCentreX [9] = cardCentreX [5] + card [1].getWidth () / 2;
cardCentreX [10] = cardCentreX [6] - card [1].getWidth () / 2;
cardCentreX [11] = cardCentreX [7] - card [1].getWidth () / 2;
cardCentreX [12] = cardCentreX [8] - card [1].getWidth () / 2;
cardCentreX [13] = cardCentreX [9] - card [1].getWidth () / 2;
cardCentreX [14] = cardCentreX [9] + card [1].getWidth () / 2;
cardCentreX [15] = cardCentreX [10] - card [1].getWidth () / 2;
cardCentreX [16] = cardCentreX [11] - card [1].getWidth () / 2;
cardCentreX [17] = cardCentreX [12] - card [1].getWidth () / 2;
cardCentreX [18] = cardCentreX [13] - card [1].getWidth () / 2;
cardCentreX [19] = cardCentreX [14] - card [1].getWidth () / 2;
cardCentreX [20] = cardCentreX [14] + card [1].getWidth () / 2;
cardCentreX [21] = cardCentreX [15] - card [1].getWidth () / 2;
cardCentreX [22] = cardCentreX [16] - card [1].getWidth () / 2;
cardCentreX [23] = cardCentreX [17] - card [1].getWidth () / 2;
cardCentreX [24] = cardCentreX [18] - card [1].getWidth () / 2;
cardCentreX [25] = cardCentreX [19] - card [1].getWidth () / 2;
cardCentreX [26] = cardCentreX [20] - card [1].getWidth () / 2;
cardCentreX [27] = cardCentreX [20] + card [1].getWidth () / 2;
for (int i = 0 ; i < 28 ; i++)
{
cardCentreY [i] = 50;
if (i == 1 || i == 2)
{
cardCentreY [i] = cardCentreY [0] + card [1].getHeight () / 2;
}
else if (i >= 3 && i <= 5)
{
cardCentreY [i] = cardCentreY [1] + card [1].getHeight () / 2;
}
else if (i >= 6 && i <= 9)
{
cardCentreY [i] = cardCentreY [3] + card [1].getHeight () / 2;
}
else if (i >= 10 && i <= 14)
{
cardCentreY [i] = cardCentreY [6] + card [1].getHeight () / 2;
}
else if (i >= 15 && i <= 20)
{
cardCentreY [i] = cardCentreY [10] + card [1].getHeight () / 2;
}
else if (i >= 21 && i <= 27)
{
cardCentreY [i] = cardCentreY [15] + card [1].getHeight () / 2;
}
}
我不会给你答案,因为自己解决这个问题会让你成为更好的程序员,但这里是你的一个起点。
int i = 0;
int j = 5;
while (i < 21)
{
for (int k = 0; k < j; k++)
{
card [i].setPtrs (card [k], card [k + 1]);
}
j++;
i++;
}
这是在第一个循环中复制模式的代码:
public static void main(String[] args) {
int offset = 0;
int index = 0;
for (int i=1; i<7; i++) {
offset += i;
for (int j=1; j<=i; j++) {
card[index++].setPtrs(card[offset+j-1], card[offset+j]);
System.out.printf("%d %d %d\n", index++, offset+j-1, offset+j);
}
}
}
结果:
0 1 2
1 3 4
2 4 5
3 6 7
4 7 8
5 8 9
6 10 11
7 11 12
8 12 13
9 13 14
10 15 16
11 16 17
12 17 18
13 18 19
14 19 20
15 21 22
16 22 23
17 23 24
18 24 25
19 25 26
20 26 27
// x - co ords
cardCentreX [0] = 800;
cardCentreX [1] = 800 - card [1].getWidth () / 2;
cardCentreX [2] = 800 + card [1].getWidth () / 2;
int last_count = 1;
int curr_count = 0;
int last_card = 0;
boolean special = false;
for (int i=3; i<= 27; i++){
if (special){
cardCentreX [i] = cardCentreX [last_card] + card [1].getWidth () / 2;
special = false;
} else {
last_card++;
cardCentreX [i] = cardCentreX [last_card] - card [1].getWidth () / 2;
if (curr_count == last_count + 1){
last_count = curr_count;
curr_count = 0;
special = true;
}
}
应该可以。对于x-coordinates。
这个想法是你有一个重复出现的特殊 属性 每 x+1 个方程?
其中 x 是您在到达特殊行之前经过的最后行数。
此 for 循环基于您设置的数字模式,如果不清楚,请使用变量的打印语句来理解工作原理(在评论中提供):
static int j = 1;
for(int 1 = 0; i <21; i++){
card [i].setPtrs (card [j], card [j+1]);
if((j+1)%4 == 0) j-1;
j+2;
counter++;
// System.out.println("i = " +i +", j = " +j +", j+1=" +(j+1));
}
// set card X coords
static int k = 1, counter = 1; // counter is a variable used for reference
cardCentreX [0] = 800;
cardCentreX [1] = 800 - card [1].getWidth () / 2;
cardCentreX [2] = 800 + card [1].getWidth () / 2;
for(int i=3; i < 28; i++){
cardCentreX [i] = cardCentreX [k] - card [1].getWidth () / 2;
if((i - k) == (counter+1)){
k++; // increment card centre index
counter++;
}
// System.out.println("i = " +i, +", k = " +k, + ", counter = " +counter);
}
看看我是如何缩短你的代码的,你应该能够自己评估和缩短最后一个 for 循环。
cardCentrecount [0] = 800;
cardCentrecount [1] = 800 - card [1].getWidth () / 2;
cardCentrecount [2] = 800 + card [1].getWidth () / 2;
int count = 3;
int term = 2;
for ( int i=3, j=1; i<=27; i++,j++ ) {
if ( (j-1) == term ) {
cardCentrecount[i] = cardCentrecount[ --j ] + card[1].getWidth() / 2;
term += count;
count ++;
}
else
cardCentrecount[i] = cardCentrecount[j] + card[1].getWidth() / 2;
}
您没有提供最小的工作示例,这使得很难测试任何可能的解决方案以确保它们与您的代码执行相同的操作。我在 JavaScript 而不是 Java 中制作了一个简单版本的原型,这让我可以做一些简单的可视化,作为 sanity-check 我的循环正常工作。我假设您能够毫不费力地将 JavaScript 逻辑转换回 Java。
这是循环(在 Java脚本中):
var x_row_base = 250; // You used 800 here
var y = 0;
var cards = Array(28);
// Set up card pyramid
for (var i = 0, row = 0; i < cards.length; row++) {
var x = x_row_base;
for (var col = 0; col < row; i++, col++) {
cards[i] = make_card(i, row);
// set x and y position
cards[i].x = x;
cards[i].y = y;
// create links to "child" cards in next row
if (i < 21) {
cards[i].left_child_index = i + row;
cards[i].right_child_index = i + row + 1;
}
// display the card
draw_card(cards[i]);
x += CARD_X_SPACING + CARD_X_PADDING;
}
y += CARD_Y_SPACING;
x_row_base -= (CARD_X_SPACING + CARD_X_PADDING) / 2;
}
您可以使用下面的代码片段 (or on jsfiddle) 进行尝试。 parent/child 卡片链接通过将鼠标悬停在父卡片上时更改子卡片的颜色来显示。这是 Java脚本可视化在我的笔记本电脑上的截图:
function draw_card(card) {
var $card = $("<div/>");
$card.text(card.label);
$card.addClass('card');
$card.css({
'left': card.x,
'top': card.y,
'z-index': card.z
});
$card.appendTo($('#my_canvas'));
card.rect = $card;
// last row doesn't actually have children
if (card.left_child_index) {
$card.mouseover(function() {
cards[card.left_child_index].rect.addClass("child");
cards[card.right_child_index].rect.addClass("child");
});
$card.mouseout(function() {
cards[card.left_child_index].rect.removeClass("child");
cards[card.right_child_index].rect.removeClass("child");
});
}
}
function make_card(label, z) {
return {
label: label,
z: z
};
}
var CARD_X_PADDING = 4;
var CARD_X_SPACING = 50;
var CARD_Y_SPACING = 45;
var x_row_base = 250; // You used 800 here
var y = 0;
var cards = Array(28);
// Set up card pyramid
for (var i = 0, row = 0; i < cards.length; row++) {
var x = x_row_base;
for (var col = 0; col < row; i++, col++) {
cards[i] = make_card(i, row);
// set x and y position
cards[i].x = x;
cards[i].y = y;
// create links to "child" cards in next row
if (i < 21) {
cards[i].left_child_index = i + row;
cards[i].right_child_index = i + row + 1;
}
// display the card
draw_card(cards[i]);
x += CARD_X_SPACING + CARD_X_PADDING;
}
y += CARD_Y_SPACING;
x_row_base -= (CARD_X_SPACING + CARD_X_PADDING) / 2;
}
.card {
border: 1px solid black;
text-align: center;
position: absolute;
width: 50px;
padding: 20px 0px;
background-color: gray;
font-size: 15px;
cursor: pointer;
}
.child {
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="my_canvas"></div>
对于我的学校项目,我们正在创建一个纸牌游戏,我有一些硬编码的代码块。我希望有人能够在我附加的代码中看到一个模式,并能够用 for 循环压缩它。
// set the pointers of the cards in the pyramid
card [0].setPtrs (card [1], card [2]);
card [1].setPtrs (card [3], card [4]);
card [2].setPtrs (card [4], card [5]);
card [3].setPtrs (card [6], card [7]);
card [4].setPtrs (card [7], card [8]);
card [5].setPtrs (card [8], card [9]);
card [6].setPtrs (card [10], card [11]);
card [7].setPtrs (card [11], card [12]);
card [8].setPtrs (card [12], card [13]);
card [9].setPtrs (card [13], card [14]);
card [10].setPtrs (card [15], card [16]);
card [11].setPtrs (card [16], card [17]);
card [12].setPtrs (card [17], card [18]);
card [13].setPtrs (card [18], card [19]);
card [14].setPtrs (card [19], card [20]);
card [15].setPtrs (card [21], card [22]);
card [16].setPtrs (card [22], card [23]);
card [17].setPtrs (card [23], card [24]);
card [18].setPtrs (card [24], card [25]);
card [19].setPtrs (card [25], card [26]);
card [20].setPtrs (card [26], card [27]);
// set card X coords
cardCentreX [0] = 800;
cardCentreX [1] = 800 - card [1].getWidth () / 2;
cardCentreX [2] = 800 + card [1].getWidth () / 2;
cardCentreX [3] = cardCentreX [1] - card [1].getWidth () / 2;
cardCentreX [4] = cardCentreX [2] - card [1].getWidth () / 2;
cardCentreX [5] = cardCentreX [2] + card [1].getWidth () / 2;
cardCentreX [6] = cardCentreX [3] - card [1].getWidth () / 2;
cardCentreX [7] = cardCentreX [4] - card [1].getWidth () / 2;
cardCentreX [8] = cardCentreX [5] - card [1].getWidth () / 2;
cardCentreX [9] = cardCentreX [5] + card [1].getWidth () / 2;
cardCentreX [10] = cardCentreX [6] - card [1].getWidth () / 2;
cardCentreX [11] = cardCentreX [7] - card [1].getWidth () / 2;
cardCentreX [12] = cardCentreX [8] - card [1].getWidth () / 2;
cardCentreX [13] = cardCentreX [9] - card [1].getWidth () / 2;
cardCentreX [14] = cardCentreX [9] + card [1].getWidth () / 2;
cardCentreX [15] = cardCentreX [10] - card [1].getWidth () / 2;
cardCentreX [16] = cardCentreX [11] - card [1].getWidth () / 2;
cardCentreX [17] = cardCentreX [12] - card [1].getWidth () / 2;
cardCentreX [18] = cardCentreX [13] - card [1].getWidth () / 2;
cardCentreX [19] = cardCentreX [14] - card [1].getWidth () / 2;
cardCentreX [20] = cardCentreX [14] + card [1].getWidth () / 2;
cardCentreX [21] = cardCentreX [15] - card [1].getWidth () / 2;
cardCentreX [22] = cardCentreX [16] - card [1].getWidth () / 2;
cardCentreX [23] = cardCentreX [17] - card [1].getWidth () / 2;
cardCentreX [24] = cardCentreX [18] - card [1].getWidth () / 2;
cardCentreX [25] = cardCentreX [19] - card [1].getWidth () / 2;
cardCentreX [26] = cardCentreX [20] - card [1].getWidth () / 2;
cardCentreX [27] = cardCentreX [20] + card [1].getWidth () / 2;
for (int i = 0 ; i < 28 ; i++)
{
cardCentreY [i] = 50;
if (i == 1 || i == 2)
{
cardCentreY [i] = cardCentreY [0] + card [1].getHeight () / 2;
}
else if (i >= 3 && i <= 5)
{
cardCentreY [i] = cardCentreY [1] + card [1].getHeight () / 2;
}
else if (i >= 6 && i <= 9)
{
cardCentreY [i] = cardCentreY [3] + card [1].getHeight () / 2;
}
else if (i >= 10 && i <= 14)
{
cardCentreY [i] = cardCentreY [6] + card [1].getHeight () / 2;
}
else if (i >= 15 && i <= 20)
{
cardCentreY [i] = cardCentreY [10] + card [1].getHeight () / 2;
}
else if (i >= 21 && i <= 27)
{
cardCentreY [i] = cardCentreY [15] + card [1].getHeight () / 2;
}
}
我不会给你答案,因为自己解决这个问题会让你成为更好的程序员,但这里是你的一个起点。
int i = 0;
int j = 5;
while (i < 21)
{
for (int k = 0; k < j; k++)
{
card [i].setPtrs (card [k], card [k + 1]);
}
j++;
i++;
}
这是在第一个循环中复制模式的代码:
public static void main(String[] args) {
int offset = 0;
int index = 0;
for (int i=1; i<7; i++) {
offset += i;
for (int j=1; j<=i; j++) {
card[index++].setPtrs(card[offset+j-1], card[offset+j]);
System.out.printf("%d %d %d\n", index++, offset+j-1, offset+j);
}
}
}
结果:
0 1 2
1 3 4
2 4 5
3 6 7
4 7 8
5 8 9
6 10 11
7 11 12
8 12 13
9 13 14
10 15 16
11 16 17
12 17 18
13 18 19
14 19 20
15 21 22
16 22 23
17 23 24
18 24 25
19 25 26
20 26 27
// x - co ords
cardCentreX [0] = 800;
cardCentreX [1] = 800 - card [1].getWidth () / 2;
cardCentreX [2] = 800 + card [1].getWidth () / 2;
int last_count = 1;
int curr_count = 0;
int last_card = 0;
boolean special = false;
for (int i=3; i<= 27; i++){
if (special){
cardCentreX [i] = cardCentreX [last_card] + card [1].getWidth () / 2;
special = false;
} else {
last_card++;
cardCentreX [i] = cardCentreX [last_card] - card [1].getWidth () / 2;
if (curr_count == last_count + 1){
last_count = curr_count;
curr_count = 0;
special = true;
}
}
应该可以。对于x-coordinates。 这个想法是你有一个重复出现的特殊 属性 每 x+1 个方程? 其中 x 是您在到达特殊行之前经过的最后行数。
此 for 循环基于您设置的数字模式,如果不清楚,请使用变量的打印语句来理解工作原理(在评论中提供):
static int j = 1;
for(int 1 = 0; i <21; i++){
card [i].setPtrs (card [j], card [j+1]);
if((j+1)%4 == 0) j-1;
j+2;
counter++;
// System.out.println("i = " +i +", j = " +j +", j+1=" +(j+1));
}
// set card X coords
static int k = 1, counter = 1; // counter is a variable used for reference
cardCentreX [0] = 800;
cardCentreX [1] = 800 - card [1].getWidth () / 2;
cardCentreX [2] = 800 + card [1].getWidth () / 2;
for(int i=3; i < 28; i++){
cardCentreX [i] = cardCentreX [k] - card [1].getWidth () / 2;
if((i - k) == (counter+1)){
k++; // increment card centre index
counter++;
}
// System.out.println("i = " +i, +", k = " +k, + ", counter = " +counter);
}
看看我是如何缩短你的代码的,你应该能够自己评估和缩短最后一个 for 循环。
cardCentrecount [0] = 800;
cardCentrecount [1] = 800 - card [1].getWidth () / 2;
cardCentrecount [2] = 800 + card [1].getWidth () / 2;
int count = 3;
int term = 2;
for ( int i=3, j=1; i<=27; i++,j++ ) {
if ( (j-1) == term ) {
cardCentrecount[i] = cardCentrecount[ --j ] + card[1].getWidth() / 2;
term += count;
count ++;
}
else
cardCentrecount[i] = cardCentrecount[j] + card[1].getWidth() / 2;
}
您没有提供最小的工作示例,这使得很难测试任何可能的解决方案以确保它们与您的代码执行相同的操作。我在 JavaScript 而不是 Java 中制作了一个简单版本的原型,这让我可以做一些简单的可视化,作为 sanity-check 我的循环正常工作。我假设您能够毫不费力地将 JavaScript 逻辑转换回 Java。
这是循环(在 Java脚本中):
var x_row_base = 250; // You used 800 here
var y = 0;
var cards = Array(28);
// Set up card pyramid
for (var i = 0, row = 0; i < cards.length; row++) {
var x = x_row_base;
for (var col = 0; col < row; i++, col++) {
cards[i] = make_card(i, row);
// set x and y position
cards[i].x = x;
cards[i].y = y;
// create links to "child" cards in next row
if (i < 21) {
cards[i].left_child_index = i + row;
cards[i].right_child_index = i + row + 1;
}
// display the card
draw_card(cards[i]);
x += CARD_X_SPACING + CARD_X_PADDING;
}
y += CARD_Y_SPACING;
x_row_base -= (CARD_X_SPACING + CARD_X_PADDING) / 2;
}
您可以使用下面的代码片段 (or on jsfiddle) 进行尝试。 parent/child 卡片链接通过将鼠标悬停在父卡片上时更改子卡片的颜色来显示。这是 Java脚本可视化在我的笔记本电脑上的截图:
function draw_card(card) {
var $card = $("<div/>");
$card.text(card.label);
$card.addClass('card');
$card.css({
'left': card.x,
'top': card.y,
'z-index': card.z
});
$card.appendTo($('#my_canvas'));
card.rect = $card;
// last row doesn't actually have children
if (card.left_child_index) {
$card.mouseover(function() {
cards[card.left_child_index].rect.addClass("child");
cards[card.right_child_index].rect.addClass("child");
});
$card.mouseout(function() {
cards[card.left_child_index].rect.removeClass("child");
cards[card.right_child_index].rect.removeClass("child");
});
}
}
function make_card(label, z) {
return {
label: label,
z: z
};
}
var CARD_X_PADDING = 4;
var CARD_X_SPACING = 50;
var CARD_Y_SPACING = 45;
var x_row_base = 250; // You used 800 here
var y = 0;
var cards = Array(28);
// Set up card pyramid
for (var i = 0, row = 0; i < cards.length; row++) {
var x = x_row_base;
for (var col = 0; col < row; i++, col++) {
cards[i] = make_card(i, row);
// set x and y position
cards[i].x = x;
cards[i].y = y;
// create links to "child" cards in next row
if (i < 21) {
cards[i].left_child_index = i + row;
cards[i].right_child_index = i + row + 1;
}
// display the card
draw_card(cards[i]);
x += CARD_X_SPACING + CARD_X_PADDING;
}
y += CARD_Y_SPACING;
x_row_base -= (CARD_X_SPACING + CARD_X_PADDING) / 2;
}
.card {
border: 1px solid black;
text-align: center;
position: absolute;
width: 50px;
padding: 20px 0px;
background-color: gray;
font-size: 15px;
cursor: pointer;
}
.child {
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="my_canvas"></div>