2013-10-31 170 views
0

一個簡單的算法有兩個類。試圖弄清楚爲什麼它不會打印任何東西。可能很明顯,但我看不到它。 該程序需要2個輸入,一個字符串和一個Int。它重複字符串int等於的次數。爲什麼這個循環不打印任何東西?

MAIN:

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

    System.out.print("Enter the string you want to repeat: "); 
    String str = input.nextLine(); 
    input.nextLine();//Clear scanner memory 

    System.out.print("Enter the amount of times you want it to repeat: "); 
    int repeat = input.nextInt(); 

    references.repeat(str, repeat); 
} 
} 

第二類:

public void repeat(String str, int n) { 
for (int repeatNum = n; repeatNum > 0; repeatNum--) { 
    System.out.println(str); 
}   
} 
+1

你輸入了什麼值?你給 – nhgrif

+0

代碼看起來不錯 – iluxa

+1

如果有可能,你能提供一個更完整的代碼示例(可能沒有任何增加或改變運行一個)?如果沒有提供,那麼閱讀有點困難,如果沒有它,可能會有一些細節我們無法實現。 –

回答

1

由於以前的答案被刪除:

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

     System.out.print("Enter the string you want to repeat: "); 
     String str = input.nextLine(); 


     System.out.print("Enter the amount of times you want it to repeat: "); 
     int repeat = input.nextInt(); 

     System.out.println(str); 
     System.out.println(repeat); 
    } 
} 

輸出:

Enter the string you want to repeat: dererer 
Enter the amount of times you want it to repeat: 5 

dererer 
5 

如果您沒有此輸出,那是因爲您的文章中未詳細說明本地化問題。

如果你做的正是這一點,你還沒有得到預期的輸出:編輯您的帖子和詳細你把確切步驟。

相關問題