0
Input a number and display in words like(123- one two three ) how i do it.please help me.
5 Respostas
+ 9
@Atul Mishra: That would be a mess and doesn't looks good still :D
I have a possibly better solution (why not use the input number as a String?) :
import java.util.*;
class getAlpha{
void getNum(String ch)
{
for(int i = 0; i < ch.length(); i++) {
switch (ch.charAt(i)) {
case '0':
System.out.print("Zero");
break;
case '1':
System.out.print(" One");
break;
case '2':
System.out.print(" Two");
break;
case '3':
System.out.print(" Three");
break;
case '4':
System.out.print(" Four");
break;
case '5':
System.out.print(" Five");
break;
case '6':
System.out.print(" Six");
break;
case '7':
System.out.print(" Seven");
break;
case '8':
System.out.print(" Eight");
break;
case '9':
System.out.print(" Nine");
break;
default:
break;
}
}
}
}
class Test{
public static void main(String[] args) {
int a;
Scanner sc = new Scanner(System.in);
a= sc.nextInt();
System.out.println(a);
getAlpha ga = new getAlpha();
ga.getNum(Integer.toString(a));
}
}
+ 11
Try it yourself first! Use conditional statements to work with numbers. For eg.,
if(number == 1) {
inWords = "one";
}
Or create an array of numbers as well as words and then try to match indexes with respectives nums...For instance :
https://code.sololearn.com/cGmC2o6iuENg/?ref=app
+ 1
https://code.sololearn.com/cddjtZKv7FKp/?ref=app
+ 1
this is what I want
+ 1
thanks @dev