Saturday, June 21, 2014

Java Exercise Nr. 51 (Characters)

Write a program that receives an ASCII code (an integer between  0 and  127 ) and displays its character.

Solution:

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int ch;

System.out.print("Enter an ASCII code:");
ch = s.nextInt();

if(ch < 0 || ch > 127) {
System.out.print("Invalid data. Number must be between 0 and 127.");
} else {
System.out.print("The character for ASCII code " + ch + " is " + (char)ch);
}
}

No comments:

Post a Comment

Author