158k views
2 votes
In Java

Write a program which includes a method named numToText(). It has a single parameter. The method takes digit (0-9), and depending on the input, returns the digit as a word (in English). Output the result from main().

User Kbariotis
by
8.2k points

1 Answer

3 votes

Answer:

class Main {

public static String numToText(int digit) {

String[] numbers = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

return numbers[digit % 10];

}

public static void main(String[] args) {

for(int i=0; i<10; i++) {

System.out.println(i+" = "+numToText(i));

}

}

}

Step-by-step explanation:

I clip the input on being 0-9 by taking it modulo 10. You could also create error handling for that if desired.

User Jarchuleta
by
7.7k points

No related questions found

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.