2010-12-12 54 views
2

我需要編寫一個接受兩個int作爲參數,分鐘和最大值的方法。在第一行,我需要打印該範圍內的所有數字(包括)。在下一行中,我以min + 1開頭,將所有數字打印到最大值,然後返回到範圍的前部並打印最小值。下一行我以min + 2開頭,依此類推,直到我用範圍中的每個數字開始重複這個開始。很難解釋,這裏有兩個例子:假設我將1和5作爲最小和最大參數傳遞。我想的方法來打印:我不知道如何重置循環(請參閱示例)

12345 
23451 
34512 
45123 
51234 

或者,如果3和9過去了,我希望這樣的:

3456789 
4567893 
5678934 
6789345 
7893456 
8934567 
9345678 

我已經試過各種事情,我敢肯定有一個簡單的方法來做到這一點,我沒有意識到。我應該這樣做沒有數組或arrayLists。我認爲我有一個很好的工作基礎,但我無法弄清楚從哪裏出發。我的基本代碼打印此:

12345 
2345 
345 
45 
5 

這:

3456789 
456789 
56789 
6789 
789 
89 
9 

我難倒。這裏是我的代碼:

public void printSquare(int min, int max){ 
    for (int i=min; i<=max; i++){ 
     for (int j=i; j<=max; j++){ 
     System.out.print(j);   
     } 
    System.out.println(); 
    } 
} 
+0

聽起來這可能是遞歸的。 – Falmarri 2010-12-12 23:30:35

+0

這不是家庭作業,它來自我一直在閱讀的網站上的一些java示例問題。我不在學校,我只是爲自己學習java =)我會考慮Peter和Raskolnikov的答案,看看我能否自己得出結論。 – Bots 2010-12-13 00:32:41

回答

2

您應該考慮每行需要多少值,然後確定這些值應該是多少。沒有給你解決方案,它很難做得更清楚。

讓我們知道你是如何去。

+0

更確切地說:就那個點的循環計數器的值而言,我們如何確定這些值? – 2010-12-13 00:15:39

+0

這是一個非常簡單的表達,所以我原以爲你可能能夠解決它。但是,如果你不能看看@ taxman的解決方案。 – 2010-12-13 10:03:13

0

想想如何打印缺少的數字。答案如下,如果你不能拿出它,你可以檢查它。


這也應該打印缺少的部分:

public void printSquare(int min, int max){ 
    for (int i=min; i<=max; i++){ 
     for (int j=i; j<=max; j++){ 
     System.out.print(j);   
     } 
     for (int k=0; k<i-min; k++){ 
     System.out.print(min+k);   
     } 
     System.out.println(); 
    } 
} 
+0

你可以重新格式化,使可讀性? – Milhous 2010-12-12 23:37:56

0

我沒有跑這一個,但它可能工作:

public void printSquare(int min, int max){ 

    int dif = max - min; 
    for (int i=min; i<=max; i++){ 

     for (int j=i; j <= i+dif ; j++){ 
     int temp = j; 
     if (temp > max) temp = temp - max; 
     System.out.print(temp);   
     } 

    System.out.println(); 
    } 

} 
0

嘗試,只是轉移的數組,像這樣:

static void Main(string[] args) 
    { 
     // this will work equally well with numbers letters or other types of characters 
     int[] nums = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 
     String a = "hello"; 
     for (int i = 0; i < nums.Length; i++) 
     { 
      int j = 5; 
      int num = i; 
      while (j-- > 0) 
      { 
       if (num >= nums.Length) 
       { 
        num = 0; 
       } 

       // shift the loop 
       Console.Write(nums[num++]); 
      } 
      Console.WriteLine(); 
     } 
    } 
1

彼得是對的,國際海事組織是以正確的方式回答作業問題。你知道每行有多少個元素,所以你需要一個外部循環給你許多元素,這會阻止你獲得你現在看到的級聯行爲。

此時,您需要考慮內部循環,您可能會使用模數運算符(%)找到最簡單的方法。這將允許你迭代而不會超越你的目標。

你應該能夠從那裏弄清楚,而且你自己從自己的算法中找出算法要好得多,而不是從別人那裏複製它,至少在這個級別的開發中。祝你好運!

0
public class Test1{ 
    public void printSquare(int min, int max){ 
     for (int i=min; i<=max; i++){ 
      for (int j=i; j<=max; j++){ 
       System.out.print(j);  
      } 
      for(int k= min; k<i; k++){ 
       System.out.print(k);  
      } 
      //System.out.print(i-1); 
      System.out.println(); 
     } 
    } 
    public static void main(String[] args){ 
     Test1 t = new Test1(); 
     t.printSquare(1,5); 
    } 
} 
+0

嘗試爲您的答案添加解釋。 – 2012-10-26 08:48:16

3

這是一個非常簡單的實現。希望這可以幫助!

int n = max-min+1; 
    for (int i=0 ; i<n; i++){ 
    for (int j=0; j<n; j++) 
    cout<<min + (i+j)%n; 
     cout<<"\n"; 
    } 

輸出:

min = 3 | max = 9 

    3456789 
    4567893 
    5678934 
    6789345 
    7893456 
    8934567 
    9345678 
3

下面的代碼..

for i = 0 to max-min 
for j = 0 to max-min 
print min + (i+j)%n 
相關問題