我在 Eclipse 中输入了以下代码,并期望每行有十个字符。但是,我无法弄清楚为什么第一行和最后一行只有 3 个字符。任何人都可以帮忙吗?
package chapter4;
import java.util.*;
public class DisplayChars {
public static void printChars(char c1, char c2, int num){
for(int i = (int)c1; i <= (int)c2; i++){
if(i % num == 0)
System.out.println("");
System.out.print((char)i);
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("please enter two characters and the number per line");
char c1 = (char)input.next().charAt(0);
char c2 = (char)input.next().charAt(0);
int numberPerLine = input.nextInt();
printChars(c1, c2, numberPerLine);
}
}
输出如下:
please enter two characters and the number per line
a
z
10
abc
defghijklm
nopqrstuvw
xyz