2016-09-26 100 views
1

我想让我的程序每次plof变量都可以分割时写出“plof”一词。 所以:假设用户输入是可变plof = 3 我想采写:1 2 3 plof 4 5 6 plof 7 8 9 plof打印每一个字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(" "); 
} 
+1

你知道Java中'%'运算符的作用吗? –

回答

0

你如果使用语句和 '%' 经营者检查是否适合打印行分隔符。

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