简体   繁体   中英

Why is this for loop not printing anything?

making a simple algo with two classes. Trying to figure out why it won't print anything. Probably something obvious, but I cannot see it. The program takes 2 inputs, a String and an Int. It repeats the string the amount of times the int equals.

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

SECOND CLASS:

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

Since the previous answer was deleted:

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

Output:

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

dererer
5

If you're not having this output, it's because of a localized issue that is not detailed in your post.

If you do exactly this and you still don't get the output as expected: edit your post and detail the exact steps you take.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM