首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java嵌套的for循环(在内部for循环中使用if语句)

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

Stack Overflow用户
提问于 2018-10-21 05:54:22
回答 1查看 1.4K关注 0票数 2

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

代码语言:javascript
复制
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);
    }        
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-21 07:33:24

发生的情况是,由于外部循环是针对列的,并且数字被标识为even,因此它将在该列中的所有行中交替使用黑白,但对于后续的列将重置。举个例子

代码语言:javascript
复制
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

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

代码语言:javascript
复制
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对变量nOdd进行true (这是一种奇特的方式,可以说是“切换”值)

代码语言:javascript
复制
    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是行优先的,对于大型n来说,将r作为外部变量可能更好,但我假设您不会期望n有很大的值,所以不要太担心这一点。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52910442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档