在此问题中,我试图让用户输入您想运行该程序的次数。例如,我输入2-表示用户希望两次运行该程序。但是,输出(如下所示)是重复的-该程序仅运行一次。
因此,我尝试输入3作为用户想要运行程序的次数。输出是重复的,和上面的一样-但是现在程序运行了两次。
我该如何解决?
import java.util.*;
public class Lab6_Problem1 {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int loop;
int count;
System.out.println("How many times would you like to run this program?");
loop = sc.nextInt();
for (count = 0; count < loop; count++)
{
System.out.println ("Please enter your text: ");
String []in = sc.nextLine().split(" ");
for (int i = 0; i < in.length; i++) {
System.out.println (in[i]);
}
}
}
}
以下是输出:
How many times would you like to run this program?
2
Please enter your text:
Please enter your text: