[英]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. 该程序有2个输入,一个String和一个Int。 It repeats the string the amount of times the int equals. 它重复字符串等于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);
}
}
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. 如果您确实执行了此操作,但仍未获得预期的输出:编辑您的帖子并详细说明您要采取的确切步骤。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.