【问题标题】:Trying to draw axes for Quadrant I, getting Quadrant IV instead试图为象限 I 绘制轴,而不是得到象限 IV
【发布时间】:2015-02-03 20:58:09
【问题描述】:

目前,我正在尝试创建一个程序,该程序在 15x15 象限 I(坐标平面)网格中的任意位置绘制一个正方形。我一直试图让轴正确显示。

这是我到目前为止的代码:

import java.util.Scanner;

public class Question2square {
  public static void main(String[] args) {

    // Axis variables
    int yAxismin = 0;
    int yAxismax = 15;
    int xAxismin = 0;
    int xAxismax = 15;

     //Loop through all coordinates on plane using for loops

    for(int y = yAxismin; y <= yAxismax; y++)
    {
      for(int x = xAxismin; x <= xAxismax; x++)
      {
        //Draw the axis 
        if (!Axis(x,y).equals("")) {
          System.out.print(Axis (x,y));
        }
      }
    System.out.println("");
    }
  }
  // This method draws the 15x15 axis
  public static String Axis(int x, int y)
  {
   // Each if and else if statement dictates what symbol needs to go where for the axes
  // If there is nothing to be drawn, there will simply be a blank space
    if (x == 15 && y== 0) return ">";    
    else if(x == 0 && y == 15) return "^";     
    else if (x == 0 && y == 0 )return ".";    
    else if(x == 0 && y >= 0) return "|";    
    else if(x >= 0 && y==0) return "-";    
    else return "";    
  }  


/*  
// Method to be used to draw actual square
  public static ... drawSquare(...) {
  }
*/
}

不幸的是,它没有画出我想要的“L”形轴,而是显示了一个“r”形。我试图弄清楚如何正确显示轴。

我尝试翻转 for 循环,但没有帮助。我看不出还有什么可以阻止这一点。

【问题讨论】:

标签: java


【解决方案1】:

尝试反转y 循环:

for(int y = yAxismax; y >= yAxismin; y--) ...

当您的循环从“上到下”然后“从左到右”将行打印到控制台时,您希望y 的最大值排在第一位,x 的最小值排在第一位。因此,您只需要反转y 循环即可从yAxismax 转到yAxismin。结果(对于3 而不是15 的限制则为:

^ | | .-->

【讨论】:

  • 啊!做到了!我曾尝试切换 y 之前开始的位置,但忽略了将字符串更新从“y++”更改为“y--”。再次感谢安迪!这里的反应迅速和质量给我留下了深刻的印象。对我可能造成的困扰深表歉意!
  • 现在通过绘制实际的正方形来受苦。 D:
  • @KommanderKitten。不需要道歉。您可以通过对好的答案进行投票来奖励帮助您的人,并通过单击复选标记来标记您问题的最佳答案 - 这也可以为您接受的每个答案获得积分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-06
  • 2011-04-23
相关资源
最近更新 更多