【发布时间】: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 循环,但没有帮助。我看不出还有什么可以阻止这一点。
【问题讨论】:
-
这里有一个很好的例子,虽然它不是你想要的:stackoverflow.com/questions/23183344/…
标签: java