Problem: Feed Nibble Monster Till Full Write a program that generates a number in [0, 500] at the beginning -- this corresponds to how hungry the monster is -- and keeps asking the user to feed the monster until that number falls to zero. Each time the user feeds the monster a nibble, hunger decreases by the decimal value of the character (i.e. if the user feeds 'A' hunger decreases by 65). But when the user feeds the monster some character that isn't a nibble, the hunger increases by the decimal value of the character (since puking depletes energy). Use while loop. Sample runs: Notice the loop exits after one iteration, because hunger was very low and one nibble made the monster full: wer on DESKTOP ZIONDUL MINGWOT / C/ COUT SES/CSZU11/ Code $ java NibbleMonsterwithLoopTill Full Monster hungry :E HUNGER: 2 Feed monster nibble :o a yum! Monster full :). You may go. weron@DESKTOP-2T8KDUL MINGW64 /c/courses/cs2011/code Notice hunger increasing after non-nibble (pink highlight): Weron DESKTOP 21IONDOL MINOWOT / C/ COUT SES/CSZUII/ CODE INOWOT $ java NibbleMonsterwith LoopTill Full Monster hungry :E HUNGER: 384 Feed monster nibble :o b yum! HUNGER: 286 Feed monster nibble :0 EWANI
Problem: Feed Nibble Monster Till Full
Write a program that generates a number in [0, 500] at the beginning -- this corresponds to how hungry the monster is -- and keeps asking the user to feed the monster until that number falls to zero.
Each time the user feeds the monster a nibble, hunger decreases by the decimal value of the character (i.e. if the user feeds 'A' hunger decreases by 65). But when the user feeds the monster some character that isn't a nibble, the hunger increases by the decimal value of the character (since puking depletes energy).
Use while loop.
Sample runs:
Notice the loop exits after one iteration, because hunger was very low and one nibble made the monster full:
Notice hunger increasing after non-nibble (pink highlight):
Notice that the program just keeps going when the user feeds the monster only non-nibbles. Do you think the program will keep running forever if the user never gives the monster nibbles?
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images