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

Neurulation and the formation of the neural tube
Neurulation and the formation of the neural tubeNeurulation and the formation of the neural tube
Neurulation and the formation of the neural tube
SaadHumayun7
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 
IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff
17thcssbs2
 
ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyes
ashishpaul799
 

Recently uploaded (20)

Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
 
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdfPost Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
 
factors influencing drug absorption-final-2.pptx
factors influencing drug absorption-final-2.pptxfactors influencing drug absorption-final-2.pptx
factors influencing drug absorption-final-2.pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
 
Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptx
 
....................Muslim-Law notes.pdf
....................Muslim-Law notes.pdf....................Muslim-Law notes.pdf
....................Muslim-Law notes.pdf
 
Neurulation and the formation of the neural tube
Neurulation and the formation of the neural tubeNeurulation and the formation of the neural tube
Neurulation and the formation of the neural tube
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...
 
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdfPost Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
 
IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
 
ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyes
 
Championnat de France de Tennis de table/
Championnat de France de Tennis de table/Championnat de France de Tennis de table/
Championnat de France de Tennis de table/
 

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.