【问题标题】:How do I change the code to have it output the whole array instead of only the first line?如何更改代码以使其输出整个数组而不仅仅是第一行?
【发布时间】:2020-09-07 12:26:21
【问题描述】:

此刻代码的输出是矩形设计和数组的第一行重复。想要的输出是矩形设计和整个数组,而不仅仅是第一行。

public class design
{
public static void main (String[] args)
{
    JFrame window = new JFrame ("Game Screen");
    window.getContentPane ().add (new drawing ());
    window.setSize (500, 500);
    window.setVisible (true);
    window.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}
class drawing extends JComponent
{
public void paint (Graphics g)
{
    int[] [] word = {{5, 3, 0, 0, 7, 0, 0, 0, 0},
            {6, 0, 0, 1, 9, 5, 0, 0, 0},
            {0, 9, 8, 0, 0, 0, 0, 6, 0},
            {8, 0, 0, 0, 6, 0, 0, 0, 3},
            {4, 0, 0, 8, 0, 3, 0, 0, 1},
            {7, 0, 0, 0, 2, 0, 0, 0, 6},
            {0, 6, 0, 0, 0, 0, 2, 8, 0},
            {0, 0, 0, 4, 1, 9, 0, 0, 5},
            {0, 0, 0, 0, 9, 0, 0, 7, 9}};
    int r = 0;
    int c = 0;
    Graphics2D g2 = (Graphics2D) g;
    Rectangle rect;
    for (int x = 5 ; x < 450 ; x += 50)
    {
        for (int y = 5 ; y < 450 ; y += 50)
        {
            rect = new Rectangle (x, y, 50, 50);
            g2.draw (rect);
            g.drawString (Integer.toString (word [r] [c]), x + 25, y + 25);
        }
        c++;
        if (c == 9)
        {
            c = 0;
            r++;
        }
    }
    rect = new Rectangle (150, 5, 150, 450);
    g2.draw (rect);
    rect = new Rectangle (5, 150, 450, 150);
    g2.draw (rect);
}
}

【问题讨论】:

    标签: java


    【解决方案1】:

    您的逻辑的问题是c 的增量不跟随y(y 轴)的增量。即根据您的逻辑c 代表二维数组中的y 轴,但您在x 轴的for 循环中递增它。解决方案是,将 c 增量的逻辑移到内部 for 循环中

    for (int x = 5; x < 450; x += 50) {
            for (int y = 5; y < 450; y += 50) {
                rect = new Rectangle(x, y, 50, 50);
                g2.draw(rect);
                g.drawString(Integer.toString(word[r][c]), x + 25, y + 25);
                c++;
            }
            if (c == 9) {
                c = 0;
                r++;
            }
        }
    

    由于您的循环只会增加 9 次,因此不需要将 if 条件与 c++ 一起移动。

    【讨论】:

      【解决方案2】:

      尝试在调用g.drawString 后将r++; 放入第二个for 循环中。 r 也必须在第一个 for 循环内和进入第二个循环之前设置为 0。

      for (int x = 5 ; x < 450 ; x += 50){
          r = 0;
          for (int y = 5 ; y < 450 ; y += 50){
              rect = new Rectangle (x, y, 50, 50);
              g2.draw (rect);
              g.drawString (Integer.toString (word [r] [c]), x + 25, y + 25);
              r++;
          }
          c++;
      }
      

      在循环中仅使用两个变量 x 和 y 在您的情况下,并且仅将它们递增一,这样会更具可读性(在我看来也是合乎逻辑的)。您可以使用它们来计算矩形的位置并放置数字:

      for (int x = 0 ; x < 9 ; x++){
          for (int y = 0 ; y < 9 ; y++){
              rect = new Rectangle (5+x*50, 5+y*50, 50, 50);
              g2.draw (rect);
              g.drawString (Integer.toString (word [y] [x]), 5+x*50 + 25, 5+y*50 + 25);
              r++;
          }
          c++;
      }
      

      避免使用幻数,最好定义常量变量并给它们一个合适的名称。然后您可以轻松更改它们的值,并且您的代码更清晰。

      What are "magic numbers" in computer programming?

      【讨论】:

        猜你喜欢
        • 2019-02-01
        • 2020-09-08
        • 2018-11-21
        • 2021-12-31
        • 2022-10-06
        • 1970-01-01
        • 2019-11-05
        • 2013-12-15
        • 1970-01-01
        相关资源
        最近更新 更多