繁体   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