2013-02-17 102 views
0

這是一個家庭作業問題,所以我想要幫助,而不是回答。如何在嵌套循環中反轉一串數字

我想根據用戶輸入的數字創建2個三角形。

"Enter a number between 2-9: "3" 
1 
12 
123 

    1 
21 
321 

IE2:

"Enter a number between 2-9: "5" 
1 
12 
123 
1234 
12345 

    1 
    21 
    321 
4321 
54321 

我已經能夠獲得第一個三角形完整。但是當我添加我的嵌套循環時,它使用從嵌套循環開發的數字來混淆我的第一個三角形。它還將所有數字放在一條直線上。我嘗試了不同的嵌套循環的變體,甚至嘗試了與StringBuilder混合,但仍然不成功。 下面是我在到目前爲止的代碼:

import java.util.Scanner; 

public class NestedLoops 
{ 
    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 

     System.out.print("Enter a Number between 2-9: "); 
     int width = input.nextInt(); 

     String r = ""; 
     for (int i = 1; i <= width; i++) 
     { 
      r = r + i; 
      System.out.println(r); 

     } 

    } 

} 

我再次尋求幫助/理解,而不僅僅是一個答案。

+0

請懸停在'家庭作業'標籤,並閱讀它說什麼。 – Xymostech 2013-02-17 03:30:06

+0

你需要使用嵌套循環進行賦值嗎?你可以用兩個循環來解決這個問題,而不用嵌套它們,或者用一些更復雜的字符串操作來解決。 – twain249 2013-02-17 03:32:41

+0

@Xymostech,不能使用學校。建議? – 2013-02-17 03:32:56

回答

0

嘗試

int width = 5; 
    // for all lines; number of lines = width 
    for (int line = 1; line <= width; line++) { 
     // print numbers from 1 to current line number 
     for (int n = 1; n <= line; n++) { 
      System.out.print(n); 
     } 
     // end of line 
     System.out.println(); 
    } 
    // add empty line between triangles 
    System.out.println(); 
    // for all lines; number of lines = width 
    for (int line = 1; line <= width; line++) { 
     // printing padding spaces, number of spaces = with - line number 
     int nSpaces = width - line; 
     for (int i = 0; i < nSpaces; i++) { 
      System.out.print(" "); 
     } 
     // print numbers from number of current line to 1 
     for (int n = line; n >= 1; n--) { 
      System.out.print(n); 
     } 
     // end of line 
     System.out.println(); 
    } 
+0

請忽略我最後的評論,我沒有看到你在第二個for循環中將行設置回1。 – SkyVar 2013-02-17 04:09:42

+0

這是Java規則:in for(int line = 1; line <= width; line ++){..} var line僅在block {}內可見。它與C++不同 – 2013-02-17 04:15:15

0

你需要使用一個隊列。 例如。 http://docs.oracle.com/javase/1.5.0/docs/api/java/util/LinkedList.html

將數字編號,直到達到最大值,然後開始征服它們。

而當你出列,則需要應用相反

Queue<String> q = new LinkedList<String>(); 
     for (int i = 1; i <= width; i++) 
     { 
      r = r + i; 
      q.add(r); 
      System.out.println(r); 

     } 

     while(!q.isEmpty()){ 
      String j = q.remove(); 
      //reverse j 
      System.out.println(reverse(j)); 
     } 

我離開翻轉部爲你做:)

+0

你有什麼機會可以向我展示這個例子嗎? – SkyVar 2013-02-17 03:39:36

0

你能只是你喜歡

第一循環之後添加另一個循環
String r = ""; 
String space = ""; 
    for (int i = width; i >= 1; i--) 
    { 
     r = r + i; 
     System.out.println(r); 

    } 

試試吧。尚未測試

1

有兩個方面的問題的第二部分。

  1. 您需要以相反的順序號碼生成字符串:

    • 您可以通過在另一端添加數字做到這一點。
    • 你可以通過反轉字符串來做到這一點。
  2. 您需要安排左側有空格。

    • 您可以通過在字符串的左端添加所需數量的空格來實現此目的。
    • 您可以通過使用System.out.format(...)來完成此操作,該模板將字段中的字符串與所需數量的字符對齊。 (好吧,這是一個有點太晦澀......)

或者,你可以建立一個字符數組或字符串生成的字符串,而不是使用字符串連接。

「訣竅」是在開始切割代碼之前找出您要使用的策略。

0
public static void main(String[] args) 
{ 
    int n = 5; 

    for(int i=1; i<=n; i++) 
    {    
     for (int j=(n*2), k=n; j>1; j--) 
     { 
      if (k <= i) 
      { 
       System.out.print(k); 
      } 
      else 
      { 
       System.out.print('*'); 
      } 

      k += (j)-1 > n ? -1 : 1; 
     } 

     System.out.println(); 
    } 
} 
0

只是試圖在斯卡拉實施。忽略,如果你不喜歡它.. :-)

class Triangle extends App 
{ 
    val width = Console.readInt() 

    if (width < 2 || width > 9) 
    { 
    throw new RuntimeException() 
    } 

    var i, j = 1; 

    for (i <- 1 to width) 
    { 
    for (j <- 1 to i) 
    { 
     print(j) 
    } 
    print("\n") 
    } 

    for (i <- 1 to width) 
    { 
    for (dummy <- 1 to width-i) 
    { 
     print(" ") 
    } 
    for (j <- i to 1 by -1) 
    { 
     print(j) 
    } 
    print("\n") 
    } 
}