greetings everyone.. this is my first post.. i would just like to ask how to shorten my code.. basically, this is a working code already.. i was told that there is a way to shorten this.. the given problem was to make a working code that would get a number input from 1-15 and display it as a word(one, two..., fifteen).. /*i just shortened it here*/
our college instructor wouldnt tell us how coz he expects us to figure this out on our own.. this code was already submitted to my instructor.. so, this is not in any way a project or a school assignment anymore.. i would just like to know how to shorten my code..
to my disadvantage, i last tackled java way back in 2004.. and havent been using it since... and now, i am stuck to figuring out how to shorten my code..
here it is...
import javax.swing.*; public class NumbersToWords { public static void main(String[] args){ int n=0; String word=""; do{ word = JOptionPane.showInputDialog("Enter a Number: "); n = Integer.parseInt(word); if( n == 1 ){ JOptionPane.showMessageDialog(null, "One"); } else if( n == 2 ){ JOptionPane.showMessageDialog(null, "Two"); } else if( n == 3 ){ JOptionPane.showMessageDialog(null, "Three"); } else if( n == 4 ){ JOptionPane.showMessageDialog(null, "Four"); } else if( n == 5 ){ JOptionPane.showMessageDialog(null, "Five"); } else{ JOptionPane.showMessageDialog(null, "Invalid Number!"); } }while((n<=5) && (n>=0)); System.exit(0); } }
im hoping you guys could give me a helping hand.. like to just point me in the right direction, and i will make the codes myself.. ^_^ thank you so much