我在eclipse中輸入了以下代碼,並且每行有10個字符的預期行。但是,我無法弄清楚爲什麼第一行和最後一行只有3個字符。誰能幫忙?爲什麼for循環中10行的格式在控制檯出錯?
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
如果你的問題解決了,你應該接受一個答案。 – AsG