Java嵌套for循环(内部for循环中带有if语句)
我正在尝试用JavaFX创建一个棋盘。由输入确定的行和列。最后一个盒子必须是白色的。如果我以奇数开头,我的代码就可以工作,但如果我以偶数开头,代码就不能工作。我不确定if-else-if语句有什么问题。我已经盯着它看了好几个小时了。请帮忙Java嵌套for循环(内部for循环中带有if语句),java,if-statement,nested-loops,Java,If Statement,Nested Loops,我正在尝试用JavaFX创建一个棋盘。由输入确定的行和列。最后一个盒子必须是白色的。如果我以奇数开头,我的代码就可以工作,但如果我以偶数开头,代码就不能工作。我不确定if-else-if语句有什么问题。我已经盯着它看了好几个小时了。请帮忙 package application; import javax.swing.JOptionPane; import javax.swing.JTextField; import javafx.application.Application;
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);
}
}
包应用;
导入javax.swing.JOptionPane;
导入javax.swing.JTextField;
导入javafx.application.application;
导入javafx.scene.scene;
导入javafx.scene.control.Label;
导入javafx.scene.control.TextField;
导入javafx.scene.layout.GridPane;
导入javafx.stage.stage;
/**
使用标签的JavaFXHelloWorld。
*/
公共类VG_棋盘0扩展了应用程序
{
int columnIndex,rowIndex;
布尔节点;
@凌驾
公众假期开始(阶段)
{
//从用户获取n
int n=Integer.parseInt(JOptionPane.showInputDialog(“请输入一个数字以制作棋盘”);
//创建网格窗格
GridPane GridPane=新建GridPane();
setGridLinesVisible(true);
//创建长方体数组
TextField[]box=新的TextField[n][n];
//设置nOdd的初始值
如果(n%2==0){
nOdd=假;
系统输出打印项次(nOdd);
}
否则{
nOdd=true;
系统输出打印项次(nOdd);
}
//填充框数组,为每个框设置颜色和位置
对于(int c=0;c所发生的事情是,由于外部循环用于列,并且数字被标识为偶数
,因此该列中的所有行将交替黑白,但后续列将重置。例如
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
是否确实是,即使您已经完成了内部循环的所有迭代,然后使用true
检查变量nOdd
(这是一种奇特的方式,可以说,“切换”值)
for(int c=0;cW偶数有什么问题?当偶数不能在每个文本字段上交替颜色时,整行的颜色都相同,所以偶数会在其他每一行上交替颜色(多维文本框数组的索引)太棒了!谢谢你!小猎犬!我花了一分钟才明白!谢谢你的详细解释!@Vynce82很高兴我能帮上忙!
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