Friday, 19 August 2016

Chapter 6 Exercise 12, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

6.12 (Display characters) Write a method that prints characters using the following header:

 public static void printChars(char ch1, char ch2, int numberPerLine)

This method prints the characters between ch1 and ch2 with the specified num- bers per line. Write a test program that prints ten characters per line from 1 to Z. Characters are separated by exactly one space.

public class ProgrammingExercise6_12 {
 
 public static void main(String[] args) {
  printChars('1', 'Z', 10);
 
 }
 
 public static void printChars(char ch1, char ch2, int numberPerLine) {
  int numberOfPrinted = 0;
 
  for (; ch1 <= ch2; ch1++) {
   System.out.print(ch1 + " ");
   numberOfPrinted++;
   if (numberOfPrinted % numberPerLine == 0) {
    System.out.println();
   }
  }
 
 }
 
}

No comments :

Post a Comment