我想在java中画一条线。我将使用这些绘制三角形。我可以这样做:
1***
11**
111*
1111
我需要这样做:
1***
*1**
**1*
***1
我今天做了很多工作,我的思绪非常困惑。
你能帮帮我吗?非常感谢。编辑:我的完美答案也应该是实施Bresenham的线条绘制算法,但我在维基百科中不了解。
编辑2:我的网格代码:
String [][] matrix = new String [50][50];
for (int row = 0; row < 50; row++){
for (int column = 0; column < 50; column++){
matrix [row][column] = "*";
}
}
答案 0 :(得分:1)
public class Test
{
public static void main(String [] args)
{
int size=50;
String[][] matrix= new String [size][size];
for (int i=0; i < size; i++)
{
for (int j=0; j < size; j++)
{
if (i != j)
matrix[i][j]="*";
else
matrix[i][j]="1";
}
}
for (int i=0; i < size; i++)
{
for (int j=0; j < size; j++)
{
System.out.print(matrix[i][j]);
}
System.out.println();
}
}
}
编辑:如果已填充*
,则只需在{等于j'时生成matrix[i][j]="1";
,即if (i==j)
。
答案 1 :(得分:0)
public class MulArray {
public static void main(String[] args) {
/*
* 1*** 1** 1* 1
*/
String[][] grid = new String[5][5];
for (int row = 0; row < grid.length-1; row++) {
for (int column = 0; column < grid[row].length; column++) {
if (row == column) {
grid[row][column] = "1";
} else {
grid[row][column] = "*";
}
}
}
for (int row = 0; row < grid.length-1; row++)
for (int column = 0; column < grid[row].length; column++) {
if (column != 4) {
System.out.print(grid[row][column]);
}
else{
System.out.print("\n");
}
}
}
}