I need help. Please someone help me. Write a java program called PrintNumberInWord which printss "ONE","TWO",....,"NINE","OTHER" if the int variable "number" is 1,2,....,9, or other respectively. Use (a)a "nested if" statement (b)a "switch case" statement.
Following are my codings for the above question. (a) public class PrintNumberInWord { public static void main(String[]args) { int number=8; if(number==1) { System.out.print("ONE"); } else if(number==2) { System.out.print("TWO"); } else if(number==3) { System.out.print("THREE"); } else if(number==4) { System.out.print("FOUR"); } else if(number==5) { System.out.print("FIVE"); } else if(number==6) { System.out.print("SIX"); } else if(number==7) { System.out.print("SEVEN"); } else if(number==8) { System.out.print("EIGHT"); } else if(number==9) { System.out.print("NINE"); } else { System.out.print("OTHER"); } } }
(b)public class PrintNumberInWord1 { public static void main(String[]args) { int number=5; switch(number) { 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: System.out.print("OTHER"); } } }
Right so... before Java 5: public class JavaStringArrayTests1 { private String[] toppings = {"Cheese", "Pepperoni", "Black Olives"}; public static void main(String[] args) { int size = toppings.length; for (int i=0; i<size; i++) { System.out.println(toppings[i]); } } } After Java 5: public class JavaStringArrayTests1 { private String[] toppings = {"Cheese", "Pepperoni", "Black Olives"}; public static void main(String[] args) { int size = toppings.length; for (String s: toppings) { System.out.println(s); } } } Does that help?
Oops... sorry... didn't even read what you need. But what is required to use seems a bit clunky and not good practice I would imagine. I would be easier to use the following if you are given a number: int number=5; private String[] numbers= {"ONE","TWO",....,"NINE"}; if(number>9) { System.out.println("OTHER"); } else { System.out.println(numbers[number-1]); }
Thanks a lot for the help @chris2332. But they want us to use only "nested if" not arrays. Is my answer right for the question? I am not sure.
Err... yes... the code is straight forward for if statements or switch/case statements!
Isn't nested if this if(a>0) { if(b<0) { } } Hw do we use this for the above queastion? That's vat is confusing me:(
@Chris2332
it wouldn't seem necessary, but you could force a nesting by first checking for the "other" case where the int is not 0-9, and then if it is 0-9, run through your print ONE, etc. cases.
That would be nested, but it seems weird to nest something like that... like you said, it makes more sense when you have two separate conditionals, not just different outcomes of the same conditional.
(disclaimer, I have about 6 total hours of Java time... I came here looking for Python posts and saw your thread :) )
:) Bigg thanks for your help:)
I don't know how helpful that was :) but thanks :)
Join our real-time social learning platform and learn together with your friends!