Java for loop display the ASCII character table

Question

We would like to write a program that prints the characters in the ASCII character table from ! to ~.

Display ten characters per line.

public class Main {
  public static void main(String[] args) {
    // Number of characters per line
    final int NUMBER_OF_CHARACTERS_PER_LINE = 10;
    int count = 0;  // Count the number of characters

    // Print the ASCII characters from ! to ~
    //your code here
  }//w ww  . j  a  v  a2 s . co m
}




public class Main {
  public static void main(String[] args) {
    // Number of characters per line
    final int NUMBER_OF_CHARACTERS_PER_LINE = 10;
    int count = 0;  // Count the number of characters

    // Print the ASCII characters from ! to ~
    for (int i = 33; i <= 126; i++) {
      count++;  // Increment count
      // Display 10 characters per line
      if (count % 10 == 0)
        System.out.println((char)i);
      else
        System.out.print((char)i + " ");  
    }
    System.out.println();
  }
}



PreviousNext

Related