SlideShare a Scribd company logo
1 of 2
Download to read offline
Guessing Game: Write a game program that randomly picks a number between 1 and 100, then
prompts the player (user) to guess the number. The player should enter numbers until they
successfully guess the right answer, at which time the program ends. They can also enter 0 to
give up (quit the program). A value like O which indicates the end of user input is called a
sentinel value.
Your program should count how many guesses the player entered and print the number of
guesses at the end of the game or when the player gives up. The sentinel value (0) should not be
counted as one of the predictions.
If the player guesses wrong, the game should signal the player by telling him whether his guess
was too high or too low. Try to make your game interesting by printing good responses and
hints. You should use the Random class to generate random numbers, not the Math class or
anything else.
Program Structure (Important): Your program must use a separate method to select random
numbers, which is declared as shown below. The pickNumber method should randomly select a
number from 1 to 100 and return the number. It should not do any printing or user input.
public int pickNumber()
Sample Output: Here is an example of what the output might look like for a game where the user
guesses on the 3rd try.
Hello, I am thinking of number from 1 to 100. Please enter your guess:
21
Sorry, your guess was too high, try again:
12
Sorry, your guess was too low, try again:
16
You're right! You made 3 guesses.
You can assume that the user always writes integer values, you don't need to properly handle
"junk" input. Keep your program efficient and try to minimize the amount of duplicate code. Do
not use 'static' fields or methods except in the main() method declaration. Do not use field
variables unnecessarily.
Below is sample output for a game where the user quits after 2 guesses.
Hello, I am thinking of number from 1 to 100.
Please enter your guess:
7
Sorry, your guess was too low, try again:
99
Sorry, your guess was too high, try again:
You gave up after 2 guesses.

More Related Content

More from aathmaproducts

More from aathmaproducts (20)

Kyle- a single taxpayer- worked as a freelance software engineer for t.pdf
Kyle- a single taxpayer- worked as a freelance software engineer for t.pdfKyle- a single taxpayer- worked as a freelance software engineer for t.pdf
Kyle- a single taxpayer- worked as a freelance software engineer for t.pdf
 
Introduction Inline CSS Styles are used to change the appearance of on.pdf
Introduction Inline CSS Styles are used to change the appearance of on.pdfIntroduction Inline CSS Styles are used to change the appearance of on.pdf
Introduction Inline CSS Styles are used to change the appearance of on.pdf
 
If a population has a standard deviation of 5- what is the VARIANCE of.pdf
If a population has a standard deviation of 5- what is the VARIANCE of.pdfIf a population has a standard deviation of 5- what is the VARIANCE of.pdf
If a population has a standard deviation of 5- what is the VARIANCE of.pdf
 
All shares were sold for $10 each- No dividends have been declared sin.pdf
All shares were sold for $10 each- No dividends have been declared sin.pdfAll shares were sold for $10 each- No dividends have been declared sin.pdf
All shares were sold for $10 each- No dividends have been declared sin.pdf
 
For a standard normal distribution- find c if P(z-c)-0-1408.pdf
For a standard normal distribution- find c if P(z-c)-0-1408.pdfFor a standard normal distribution- find c if P(z-c)-0-1408.pdf
For a standard normal distribution- find c if P(z-c)-0-1408.pdf
 
6- With regards to TWO of the management accounting terms listed below.pdf
6- With regards to TWO of the management accounting terms listed below.pdf6- With regards to TWO of the management accounting terms listed below.pdf
6- With regards to TWO of the management accounting terms listed below.pdf
 
2----4-0 For the past several months- per capita output has decreased.pdf
2----4-0 For the past several months- per capita output has decreased.pdf2----4-0 For the past several months- per capita output has decreased.pdf
2----4-0 For the past several months- per capita output has decreased.pdf
 
You are to draw cistercian number at the centre of canvas- The numbers.pdf
You are to draw cistercian number at the centre of canvas- The numbers.pdfYou are to draw cistercian number at the centre of canvas- The numbers.pdf
You are to draw cistercian number at the centre of canvas- The numbers.pdf
 
Using Coca-Cola- access its most recent financial statements- form 10-.pdf
Using Coca-Cola- access its most recent financial statements- form 10-.pdfUsing Coca-Cola- access its most recent financial statements- form 10-.pdf
Using Coca-Cola- access its most recent financial statements- form 10-.pdf
 
Select the statements that explain why two traits would be inherited t.pdf
Select the statements that explain why two traits would be inherited t.pdfSelect the statements that explain why two traits would be inherited t.pdf
Select the statements that explain why two traits would be inherited t.pdf
 
What do scientists identify as the reasons for catfish rely on the lat.pdf
What do scientists identify as the reasons for catfish rely on the lat.pdfWhat do scientists identify as the reasons for catfish rely on the lat.pdf
What do scientists identify as the reasons for catfish rely on the lat.pdf
 
Refer to Table 6-2- The unemployment rate in year 3 is.pdf
Refer to Table 6-2- The unemployment rate in year 3 is.pdfRefer to Table 6-2- The unemployment rate in year 3 is.pdf
Refer to Table 6-2- The unemployment rate in year 3 is.pdf
 
What steps are taken in order to run a macro on a new set of data- Mac.pdf
What steps are taken in order to run a macro on a new set of data- Mac.pdfWhat steps are taken in order to run a macro on a new set of data- Mac.pdf
What steps are taken in order to run a macro on a new set of data- Mac.pdf
 
1- Define Sex 2- Can you be in a roman and intimate relationship witho.pdf
1- Define Sex 2- Can you be in a roman and intimate relationship witho.pdf1- Define Sex 2- Can you be in a roman and intimate relationship witho.pdf
1- Define Sex 2- Can you be in a roman and intimate relationship witho.pdf
 
Which of the following is a typical activity performed during the Anal.pdf
Which of the following is a typical activity performed during the Anal.pdfWhich of the following is a typical activity performed during the Anal.pdf
Which of the following is a typical activity performed during the Anal.pdf
 
1- Picaresque Sails Co- has total assets of $140-000 and total liabili.pdf
1- Picaresque Sails Co- has total assets of $140-000 and total liabili.pdf1- Picaresque Sails Co- has total assets of $140-000 and total liabili.pdf
1- Picaresque Sails Co- has total assets of $140-000 and total liabili.pdf
 
The m and p genes are 14 m-u- apart on an autosome- An Mp-mP woman mat.pdf
The m and p genes are 14 m-u- apart on an autosome- An Mp-mP woman mat.pdfThe m and p genes are 14 m-u- apart on an autosome- An Mp-mP woman mat.pdf
The m and p genes are 14 m-u- apart on an autosome- An Mp-mP woman mat.pdf
 
QUIZ3 The number of months brand A paint lasted before fading was 10-6.pdf
QUIZ3 The number of months brand A paint lasted before fading was 10-6.pdfQUIZ3 The number of months brand A paint lasted before fading was 10-6.pdf
QUIZ3 The number of months brand A paint lasted before fading was 10-6.pdf
 
place distribution-promotion of urban outfitters Design distribution s.pdf
place distribution-promotion of urban outfitters Design distribution s.pdfplace distribution-promotion of urban outfitters Design distribution s.pdf
place distribution-promotion of urban outfitters Design distribution s.pdf
 
Prove that L(NFA) is countable-.pdf
Prove that L(NFA) is countable-.pdfProve that L(NFA) is countable-.pdf
Prove that L(NFA) is countable-.pdf
 

Recently uploaded

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 

Guessing Game- Write a game program that randomly picks a number betwe.pdf

  • 1. Guessing Game: Write a game program that randomly picks a number between 1 and 100, then prompts the player (user) to guess the number. The player should enter numbers until they successfully guess the right answer, at which time the program ends. They can also enter 0 to give up (quit the program). A value like O which indicates the end of user input is called a sentinel value. Your program should count how many guesses the player entered and print the number of guesses at the end of the game or when the player gives up. The sentinel value (0) should not be counted as one of the predictions. If the player guesses wrong, the game should signal the player by telling him whether his guess was too high or too low. Try to make your game interesting by printing good responses and hints. You should use the Random class to generate random numbers, not the Math class or anything else. Program Structure (Important): Your program must use a separate method to select random numbers, which is declared as shown below. The pickNumber method should randomly select a number from 1 to 100 and return the number. It should not do any printing or user input. public int pickNumber() Sample Output: Here is an example of what the output might look like for a game where the user guesses on the 3rd try. Hello, I am thinking of number from 1 to 100. Please enter your guess: 21 Sorry, your guess was too high, try again: 12 Sorry, your guess was too low, try again: 16 You're right! You made 3 guesses. You can assume that the user always writes integer values, you don't need to properly handle "junk" input. Keep your program efficient and try to minimize the amount of duplicate code. Do not use 'static' fields or methods except in the main() method declaration. Do not use field variables unnecessarily. Below is sample output for a game where the user quits after 2 guesses. Hello, I am thinking of number from 1 to 100.
  • 2. Please enter your guess: 7 Sorry, your guess was too low, try again: 99 Sorry, your guess was too high, try again: You gave up after 2 guesses.