Java开始if for循环中的语句被跳过而其他人工作

时间:2016-04-27 19:10:21

标签: java if-statement for-loop

又回到另一个问题,这个问题完全困扰了我,并且无法解决我的生活。

所以我之前发布了一个关于我的Java游戏的问题,我正在为一个在@MadProgrammer的帮助下修复的课程...主要是。现在有一个新问题,需要一个帖子全部到它自己的

上一篇文章: Rows and columns with multidimensional array java

问题: 在下面的代码中,我设置循环遍历变量x和y以在jPanel上创建行和列。每次循环时,它应随机地用“地形”之一标记“x,y”位置,以便以后可以用适当的彩色20x20方格“绘制”该位置。

代码运行很好,除了一件事,看起来它跳过了第一个“if语句”,标志着“terran [0]”是“floor”。当代码运行时,它“绘制”其他三个“地形”而不是一个“地板”“地形”。

我已经在这些帖子上寻找解决方案,但没有成功:
Java if statement is skipped
If statement being skipped during execution
Java - for loops being skipped
Java if-statement being skipped

这是一段可以解决问题的工作代码:

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class gamePanel extends JPanel
{   

    public gamePanel()
    {
        setBounds(115,93,480,480);
    }

    private Random generator = new Random();    

    int floor = 0; //initializes the variable floor to zero for later use
    int dirt = 1;
    int stone = 2;
    int water = 3;

    int width = 24;
    int height = 24;    
    int x, y; // my x & y variables for coordinates

    int[][] coords = new int[width][height]; //my array that I want to store the coordinates for later use in painting


    int[] terrain = {floor, dirt, stone, water}; //my terrain that will determine the color of the paint

    public void mapGen() //what should mark/generate the JPanel
    {
        for(x = 0; x < width; x++)
        {

            for(y = 0; y < height; y++)
            {

                int z = generator.nextInt(20);// part of the randomization

                if(z <= 10)
                {
                    coords[x][y] = terrain[0]; //should mark the coordinates as floor

                }

                if(z == 11)
                {
                    coords[x][y] = terrain[3];//should mark the coordinates as water
                }

                if(z >= 12 && z <= 16)
                {
                    coords[x][y] = terrain[2];//should mark the coordinates as stone
                }

                if(z >= 17 && z <= 19)
                {
                    coords[x][y] = terrain[1];//should mark the coordinates as dirt
                }

                    coords[0][0] = terrain[0]; // sets coordinate 0,0 to floor //need to have these always be floor
                    coords[23][23] = terrain[0]; // sets coordinate 24,24 to floor //^^^^^^^^^^
            }
        }


    }   


    @Override
    public void paintComponent(Graphics g)//what will paint each 20x20 square on the grid what it is assigned
    {
        super.paintComponent(g);

        for(int x = 0; x < width; x++)
        {
            for(int y = 0; y < height; y++)
            {
                mapGen();

                if(coords[x][y] == terrain[floor])//should paint the floor color at marked coordinates
                {
                    g.setColor(Color.white);
                    g.fillRect((x*20), (y*20), 20, 20); 

                }

                if(coords[x][y] == terrain[dirt]);//should paint the dirt color at marked coordinates
                {
                    g.setColor(new Color(135,102,31));
                    g.fillRect((x*20), (y*20), 20, 20);
                }

                if(coords[x][y] == terrain[stone])//should paint the stone color at marked coordinates
                {
                    g.setColor(new Color(196,196,196));
                    g.fillRect((x*20),(y*20),20,20);
                }

                if(coords[x][y] == terrain[water])//should paint the water color at marked coordinates
                {
                    g.setColor(new Color(85,199,237));
                    g.fillRect((x*20),(y*20),20,20);
                }
            }
        }

    }//end paintComponent

public static void main(String[] args)
{
    gamePanel panel = new gamePanel();
    JFrame frame = new JFrame();
    frame.setSize(500,550);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(panel);
    frame.setVisible(true);

}//end main

}// end gamePanel

请记住,我是一名新手程序员,我还在学习。所以任何不被视为“基本”代码的内容都请详细解释。

0 个答案:

没有答案