Java Question: Create a program that gets a number from a user. This number shou
ID: 3861933 • Letter: J
Question
Java Question:
Create a program that gets a number from a user. This number should be from 1 to 26. Then have the user input a phrase, with this phrase you want to add the number they entered and print out the new phrase.
For an example:
User Input: 5
User Input Expected output
A F
B G
C H
User Input: 3
User Input Expected output
A D
B E
C F
Explanation / Answer
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
if(number < 1 || number > 26){
System.out.println("Number should be between 1 - 26");
System.exit(0);
}
// read only signle character from input
char c = sc.next().charAt(0);
// (int) c -> this is to conver character to ascii number
number = number + (int) c;
// (char) number -> this is to convert back to character from ascii number
System.out.println( (char) number);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.