【问题标题】:Java Nested for loop ( with if statement in inner for loop )Java 嵌套 for 循环(内部 for 循环中带有 if 语句)
【发布时间】:2018-10-20 21:54:22
【问题描述】:

我正在尝试使用 JavaFX 创建一个棋盘格。由输入确定的行和列。最后一个盒子必须是白色的。如果我以奇数开头,我的代码有效,但如果我以偶数开头,则代码无效。我不确定 if-else if 语句有什么问题。我已经盯着它看了好几个小时了。请帮忙!

package application;    
import javax.swing.JOptionPane;
import javax.swing.JTextField;    
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;    
/** 
    Java FX Hello World using a Label.
 */    
public class VG_CheckerBoard0 extends Application
{  
    int columnIndex, rowIndex;
    boolean nOdd;
    @Override
    public void start(Stage stage)
    {               
        //get n from user
        int n= Integer.parseInt(JOptionPane.showInputDialog("Please input a number to make checkerboard: "));
         //create gridpane
        GridPane gridpane = new GridPane();
        gridpane.setGridLinesVisible(true);

        // Create array of boxes 
        TextField[][] box = new TextField[n][n];             
        //set initial value of nOdd
        if ( n%2 == 0 ){
            nOdd = false;
        System.out.println(nOdd);
        }
        else {
            nOdd = true ;
        System.out.println(nOdd);
        }
        //populate array of boxes, set color and position for each
        for(int c= 0; c<box.length;c++){
             for(int r= 0; r<box.length;r++){
                 box[c][r] = new TextField("x");
                 //System.out.println(box[c][r]);

                     if (nOdd == true){

                         box[c][r].setStyle("-fx-background-color: white;" + "-fx-border-color: black;");

                         //switch for next iteration
                         System.out.println(nOdd + " - inside if true before ");
                         nOdd = false;
                         System.out.println(nOdd + " - inside if true after");
                     }else if (nOdd == false){
                         box[c][r].setStyle("-fx-background-color: black;" + "-fx-border-color: black;");

                        //switch for next iteration
                         System.out.println(nOdd + " + inside if false before");
                         nOdd = true;
                         System.out.println(nOdd + " + inside if false after");
                        }

                 gridpane.add(box[c][r], c, r);

             }//inner
         }//outer   

        // Set label as the root of scene graph.
        Scene scene = new Scene(gridpane);   
        stage.setScene(scene);

        // Set stage title and show the stage.
        stage.setTitle("Checkerboard");
        stage.show();
    }   
    public static void main(String[] args){
        launch(args);
    }        
}

下面的示例输出(奇数作品,棋盘图案)。

【问题讨论】:

  • 偶数有什么问题?
  • 即使它不会在每个文本字段上交替颜色,它也将是整行的相同颜色,所以即使是数字,它也会每隔一行交替颜色,就像每个其他框一样(多维文本框数组的索引)

标签: java if-statement nested-loops


【解决方案1】:

发生的情况是,由于外部循环是针对列的,并且数字被标识为even,它将对该列中的所有行交替显示黑色和白色,但会为后续列重置。比如

initial value for nOdd = false
                            c = 0     c = 1 .. the process repeats again
                            |column 0|column 1|column 2|column3
it's alternating ------>    |black   |black                     nOdd = true
row-wise            |-->    |white   |white                     nOdd = false
                            |black   |black                     nOdd = true
                            |white   |white                     nOdd = false

nOdd came full circle so it starts back in black again

它适用于奇数,因为一切都正确对齐

initial value for nOdd = true
                            c = 0     c = 1 .. the process repeats again
                            |column 0|column 1|column 2
it's alternating ------>    |white   |black             nOdd = false
row-wise            |-->    |black   |white             nOdd = true
                            |white   |black             nOdd = false

nOdd started as true and ended as false so for the next column we get alternating colors

一个快速的解决方法是检查n是否确实是即使你已经完成了内部循环的所有迭代,然后xor变量nOddtrue(一种奇特的说法,“切换”值)

    for(int c= 0; c<box.length;c++){
         for(int r= 0; r<box.length;r++){
             box[c][r] = new TextField("x");
             //System.out.println(box[c][r]);

                 if (nOdd == true){

                     box[c][r].setStyle("-fx-background-color: white;" + "-fx-border-color: black;");

                     //switch for next iteration
                     System.out.println(nOdd + " - inside if true before ");
                     nOdd = false;
                     System.out.println(nOdd + " - inside if true after");
                 }else if (nOdd == false){
                     box[c][r].setStyle("-fx-background-color: black;" + "-fx-border-color: black;");

                    //switch for next iteration
                     System.out.println(nOdd + " + inside if false before");
                     nOdd = true;
                     System.out.println(nOdd + " + inside if false after");
                    }

             gridpane.add(box[c][r], c, r);

         }//inner
         if(n % 2 == 0) {
             nOdd = nOdd ^ true;
         }
     }//outer 

您还可以使用索引rc 来决定单元格的颜色。次要细节,因为 Java 是行主要的,最好将 r 作为大 n 的外部变量,但我假设您不希望 n 有很大的值,所以不要太担心这个了。

【讨论】:

  • 太棒了!谢谢StaticBeagle!我花了一分钟才明白!感谢您的详尽解释!
  • @Vynce82 很高兴我能帮上忙!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多