Hello, my question is how do I get the program to stop when the user enters q or Q in a do-while loop? (The first image is the question I'm answering and the second one is the code

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Hello, my question is how do I get the program to stop when the user enters q or Q in a do-while loop? (The first image is the question I'm answering and the second one is the code that I have so far)

import java.util.Scanner;
public class NibbleMonsterWithSentinelcControlledLoop{
public static void main(String[] args){
char ch;
boolean a=true;
Scanner sc=new Scanner(System.in);
System.out.println("Monster hungry :E");
System.out.println("Enter 'Q' when you run out of snacks.");
do{
System.out.print("Feed monster nibble :0 ");
ch=sc.next().charAt(0);
int ascii=ch;
if(ch>=65 && ch<=7®){
System.out.println("Yum!");
}
else if(ch>=48 && ch<=57){
System.out.println("m04r føod!");
}
else if(ch>=98 && ch<=102){
System.out.println("Yum!!");
}
else{
System.out.println("Ewww! :0="+ch);
}
}while(ch
System.out.println("Ewww! :o="+ch);
System.out.println("Bye Quitter");
81 && ch
113);
==
==
a=false;
}
}
Transcribed Image Text:import java.util.Scanner; public class NibbleMonsterWithSentinelcControlledLoop{ public static void main(String[] args){ char ch; boolean a=true; Scanner sc=new Scanner(System.in); System.out.println("Monster hungry :E"); System.out.println("Enter 'Q' when you run out of snacks."); do{ System.out.print("Feed monster nibble :0 "); ch=sc.next().charAt(0); int ascii=ch; if(ch>=65 && ch<=7®){ System.out.println("Yum!"); } else if(ch>=48 && ch<=57){ System.out.println("m04r føod!"); } else if(ch>=98 && ch<=102){ System.out.println("Yum!!"); } else{ System.out.println("Ewww! :0="+ch); } }while(ch System.out.println("Ewww! :o="+ch); System.out.println("Bye Quitter"); 81 && ch 113); == == a=false; } }
Problem: Feed Nibble Monster Until User Wants To Quit
Write a program in which the Nibble Monster keeps asking for nibbles until the user enters 'Q' or 'q!
Use do-while loop.
HINT: This is a sentinel-controlled loop.
Sample runs:
weron@DESKTOP-218KDUL MINGW64 /c/courses/CS2011/code
$ java NibbleMonsterwithSentinelcontrolledLoop
Monster hungry :E
Enter 'Q' when you run out of snacks.
Feed monster nibble :o 1
m04r f00d!
Feed monster nibble :o 2 +
m04r f00d!
Feed monster nibble :o e e
yum!
Feed monster nibble :o D
YUM!
Feed monster nibble :o
Ewww! :0=*
Feed monster nibble :o 7
m04r f00d!
Feed monster nibble :o q 4
Ewww! :0=q
Bye quitter.
weron@DESKTOP-218KDUL MINGW64 /c/courses/Cs2011/code
$ java NibbleMonsterwithSentinelcontrolledLoop
Monster hungry :E
Enter 'Q' when you run out of snacks.
Feed monster nibble :o Q
Ewww! :0=Q
Bye quitter.
Transcribed Image Text:Problem: Feed Nibble Monster Until User Wants To Quit Write a program in which the Nibble Monster keeps asking for nibbles until the user enters 'Q' or 'q! Use do-while loop. HINT: This is a sentinel-controlled loop. Sample runs: weron@DESKTOP-218KDUL MINGW64 /c/courses/CS2011/code $ java NibbleMonsterwithSentinelcontrolledLoop Monster hungry :E Enter 'Q' when you run out of snacks. Feed monster nibble :o 1 m04r f00d! Feed monster nibble :o 2 + m04r f00d! Feed monster nibble :o e e yum! Feed monster nibble :o D YUM! Feed monster nibble :o Ewww! :0=* Feed monster nibble :o 7 m04r f00d! Feed monster nibble :o q 4 Ewww! :0=q Bye quitter. weron@DESKTOP-218KDUL MINGW64 /c/courses/Cs2011/code $ java NibbleMonsterwithSentinelcontrolledLoop Monster hungry :E Enter 'Q' when you run out of snacks. Feed monster nibble :o Q Ewww! :0=Q Bye quitter.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education