2015-11-24 276 views
-4

我必須找到所有可能性來將n個東西分發給k個容器。容器都應該有不同的大小,所以我做了k個內部for-loops來計算每種可能性。對不起,不好的解釋,但我的英語不太好。Java簡單的for循環

現在我需要知道如何使K中循環,使其適用於2和10

感謝

+6

通過編寫代碼來表現得像那樣? – Stultuske

+5

是的,這是可能的Java。您可以開始... –

+3

您可以在循環中使用循環。如果外環運行10次,那麼內環將重複10次... – Ivar

回答

1

打破你的問題分成兩個步驟:

  1. 獲取輸入'times'from the user

  2. 運行循環'times'times

    Scanner in = new Scanner(System.in); 
    System.out.println("Enter numebr of times: "); 
    int times = in.nextInt(); 
    
    for (int i=0 ; i<times ; i++){ 
        // looping 'times' times 
        System.out.println("hello"); 
    } 
    
+0

如何刪除此? –