真心不會,越打越暈
求大神賜教
uj5u.com熱心網友回復:
沒看懂你啥意思,題目呢?
uj5u.com熱心網友回復:
給你一個比較粗糙的模板吧,你可以照著先理解一下,然后想想如何優化這個版本,讓代碼更簡潔,更好理解
//輸入為:*的最多連續點數,比如上圖所示的為3,直接控制臺輸入3,回車即可顯示結果,當然,也可以輸入其他的整數
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("輸入最多連續點數:");
int n = scan.nextInt();
int[] arr = new int[n*2+2];
int ii = n;
int jj = 0;
for(int i=0; i<arr.length; i++) {
if(i < n) {
for(int j=0; j<arr.length; j++) {
if(j < ii) {
System.out.print("*");
}else if(j >= (arr.length - ii) && j < arr.length) {
System.out.print("*");
}else {
System.out.print(" ");
}
}
ii--;
System.out.println();
}
if(n <= i && i <= (arr.length-1-n)) {
System.out.print(" ");
System.out.println();
}
if(i > (arr.length-1-n)) {
for(int j=0; j<arr.length; j++) {
if(j <= jj) {
System.out.print("*");
}else if(j >= (arr.length-1-jj)) {
System.out.print("*");
}else {
System.out.print(" ");
}
}
jj++;
System.out.println();
}
}
System.out.println("-----------------------------");
}
有不理解的,可以問我,不過要記得結帖呀。
uj5u.com熱心網友回復:
光給個效果圖,不把題目愿意說明白,實作效果會差別很大的!uj5u.com熱心網友回復:
6-4-2 / 2-4-6 兩個回圈罷了!uj5u.com熱心網友回復:
假設二維資料是一個標準矩陣(行寬=列寬),代碼如下:
public class PrintStars {
private int n = 1;
private int length;
private char[][] matrix;
private char symbol = '*';
public PrintStars(int n) {
this.n = n;
this.length = n * 2 + 2;
this.matrix = new char[this.length][this.length];
this.initArrary();
this.setStars();
}
// 初始化資料都為空白符號
private void initArrary() {
for (int i = 0; i < this.length; i++)
for (int j = 0; j < this.length; j++)
this.matrix[i][j] = ' ';
}
// 按條件設定*號
// 因為有很明顯的對稱關系,直接2層回圈搞定
// 所以設定方式如函式體內的描述
public void setStars() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
//從第0行開頭開始設定
this.matrix[i][j] = this.symbol;
//從第0行末尾開始設定
this.matrix[i][this.length - j - 1] = this.symbol;
//從最后一行開頭開始設定
this.matrix[this.length - i - 1][j] = this.symbol;
//從最后一行末尾開始設定
this.matrix[this.length - i - 1][this.length - j - 1] = this.symbol;
}
}
}
public void print() {
for (int i = 0; i < this.matrix.length; i++) {
for (int j = 0; j < this.matrix.length; j++) {
System.out.print(this.matrix[i][j]);
System.out.print('\t');
}
System.out.println();
}
}
public static void main(String[] args) {
PrintStars ps = new PrintStars(3);
ps.print();
}
}
uj5u.com熱心網友回復:
忽略5樓,以這個為準!這樣子代碼更加規范化些:
package csdn;
public class PrintStars {
private int n = 1;
private int length;
private char[][] matrix;
private char symbol = '*';
public PrintStars(int n) {
this.n = n;
this.length = n * 2 + 3;
this.matrix = new char[this.length][this.length];
this.initArrary();
}
// 初始化資料都為空白符號
private void initArrary() {
for (int i = 0; i < this.length; i++)
for (int j = 0; j < this.length; j++)
this.matrix[i][j] = ' ';
}
// 按條件設定符號
// 因為有很明顯的對稱關系,直接2層回圈搞定
// 所以設定方式如函式體內的描述
public void setSymbols() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
//從第0行開頭開始設定
this.matrix[i][j] = this.symbol;
//從第0行末尾開始設定
this.matrix[i][this.length - j - 1] = this.symbol;
//從最后一行開頭開始設定
this.matrix[this.length - i - 1][j] = this.symbol;
//從最后一行末尾開始設定
this.matrix[this.length - i - 1][this.length - j - 1] = this.symbol;
}
}
}
public void print() {
for (int i = 0; i < this.matrix.length; i++) {
for (int j = 0; j < this.matrix.length; j++) {
System.out.print(this.matrix[i][j]);
System.out.print('\t');
}
System.out.println();
}
}
public static void main(String[] args) {
PrintStars ps = new PrintStars(3);
ps.setSymbols();
ps.print();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/124622.html
標籤:Java SE
上一篇:對于一段字串 "var p='xxxx'" 怎么在java里獲得p的值
下一篇:netbean構建編譯專案失敗。
