我希望每次plof变量都可分割时,我的程序都写“ plof”一词。
因此:假设用户输入的变量为plof = 3
我想写:1 2 3 plof 4 5 6 plof 7 8 9 plof
public static void main(String[] args) {
Scanner invoer = new Scanner(System.in);
System.out.print("Wat is het \"plof\" getal? (2..9)");
int plof = invoer.nextInt();
System.out.print("Tot en met welk getal moet ik tellen?");
int telGetal = invoer.nextInt();
for(int i=0; i<= telGetal; i++) {
System.out.print(" "+i);
}
System.out.println(" ");
}
最佳答案
您必须使用if语句和'%'运算符来检查是否适合打印行分隔符。
public static void main(String[] args) {
Scanner invoer = new Scanner(System.in);
System.out.print("Wat is het \"plof\" getal? (2..9)");
int plof = invoer.nextInt();
System.out.print("Tot en met welk getal moet ik tellen?");
int telGetal = invoer.nextInt();
for(int i=1; i<= telGetal; i++) {
System.out.print(" "+i);
if (i%plof == 0)
System.out.print('\n');
}
System.out.println(" ");
}