Java嵌套的for循环(在内部for循环中使用if语句)

时间:2018-10-20 21:54:22

标签: java if-statement nested-loops

我正在尝试使用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);
    }        
}

下面的样本输出(奇数工作,CHECKERBOARD模式)。 ODD NUMBER WORKS AS EXPECTED

EVEN NUMBER DOES NOT WORK

1 个答案:

答案 0 :(得分: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的确是否正确,即使您已经经历了内循环的所有迭代,然后使用xornOdd变量true (一种奇妙的说法,“切换”值)

    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的外部变量,但我假设您不希望{{1 }},所以不必为此担心太多。