Write a program that prompts the user to enter a number within the range of 1 th
ID: 3645827 • Letter: W
Question
Write a program that prompts the user to enter a number within the range of 1 through 10. The program should then display that number in German. If the number is outside the range of 1-10, the program should display an error message.write the program using a switch statment and then also write the program using an if statement.
I have Written it in an elce if scenario but I am having less success w/ the switch.
import java.util.*;
public class Name_If
{
public static void main(String[] args)
{int num;
Scanner in=new Scanner(System.in);
System.out.println("Enter a number between 1 and 10!!!");
num=in.nextInt();
System.out.print(num +" The number you entered translated to German is - ");
if (num==1)
System.out.println("Eins!");
else if(num==2)
System.out.println("Zwei!");
else if(num==3)
System.out.println("Drei!");
else if(num==4)
System.out.println("Vier!");
else if(num==5)
System.out.println("Funf!");
else if(num==6)
System.out.println("Sechs!");
else if(num==7)
System.out.println("Sieben!");
else if(num==8)
System.out.println("Acht!");
else if(num==9)
System.out.println("Neun!");
else if(num==10)
System.out.println("Zehn!")
else
System.out.println(" an invalid entry - That number was not between 1 and 10.");
}
}
Explanation / Answer
public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.println("Enter a number: "); int num = in.nextInt(); switch(num){ case 1: System.out.println("Eins!"); break; case 2: System.out.println("Zwei!"); break; case 3: System.out.println("Drei!"); break; case 4: System.out.println("Vier!"); break; case 5: System.out.println("Funf!"); break; case 6: System.out.println("Sechs!"); break; case 7: System.out.println("Sieben!"); break; case 8: System.out.println("Acht!"); break; case 9: System.out.println("Neun!"); break; case 10: System.out.println("Zehn!"); break; default: System.out.println("ERROR! Not a valid number between 1 and 10!"); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.