public static void printChars(char ch1, char ch2, int numberPerLine)
This method prints the characters between ch1 and ch2 with the specified numbers per line. Write a test program the prints ten characters per line from 1 to Z. Character are separated by exactly one space.
import java.util.Scanner; public class C1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); //提示使用者輸入 System.out.println("請輸入起始字元:"); char startchar = input.next().charAt(0); System.out.println("請輸入結束字元:"); char endchar = input.next().charAt(0); System.out.println("請輸入每行個數:"); int numberPerLine = input.nextInt(); //呼叫printChars printChars(startchar , endchar , numberPerLine); } public static void printChars(char ch1 , char ch2 , int numberPerLine) { int a = ch1 ; int b = ch2 ; int count = 1; for(int i = a ; i <= b ; i ++) { if(count % numberPerLine == 0 ) { System.out.println((char)i + " "); count = count + 1; } else { System.out.print((char)i + " "); count = count + 1; } } } }
沒有留言:
張貼留言