ajprincess
  • ajprincess
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.
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
ajprincess
  • ajprincess
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"); } } }
ajprincess
  • ajprincess
(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"); } } }
anonymous
  • anonymous
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

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
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]); }
ajprincess
  • ajprincess
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.
anonymous
  • anonymous
Err... yes... the code is straight forward for if statements or switch/case statements!
ajprincess
  • ajprincess
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:(
ajprincess
  • ajprincess
@Chris2332
anonymous
  • anonymous
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.
anonymous
  • anonymous
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.
anonymous
  • anonymous
(disclaimer, I have about 6 total hours of Java time... I came here looking for Python posts and saw your thread :) )
ajprincess
  • ajprincess
:) Bigg thanks for your help:)
anonymous
  • anonymous
I don't know how helpful that was :) but thanks :)

Looking for something else?

Not the answer you are looking for? Search for more explanations.