簡體   English   中英

為什么此for循環不打印任何內容?

[英]Why is this for loop not printing anything?

用兩個類制作一個簡單的算法。 試圖弄清楚為什么它什么都不打印。 可能很明顯,但我看不到。 該程序有2個輸入,一個String和一個Int。 它重復字符串等於int的次數。

主要:

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);
 }          
}

由於上一個答案已刪除:

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

如果您沒有此輸出,那是由於您的帖子中未詳細介紹的本地化問題。

如果您確實執行了此操作,但仍未獲得預期的輸出:編輯您的帖子並詳細說明您要采取的確切步驟。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM