What is the method of Java to realize the game of guessing numbers?
The main content of this article is to explain "what is the method of Java to achieve the number guessing game", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the method of Java to realize the number guessing game"?
Import related modules
Import randomfrom datetime import datetime
Generate random numbers
Def random_number (): print ('*'* 30) print ('range of random numbers is 0,10') nb = random.choice ([x for x in range (11)]) return nb
Function introduction
Def intruduce (): print (this program has the following three functions: 1, start the game 2, view the bureau's historical data 3, exit the program') print ('* 30)
Guess the number
Def guess_number (nb): print (''the game begins! Warm reminder: the guess range is 10, you only have three chances, please cherish\ nguess') log = {} for i in range (3): try: guess_number = int (input ('enter guessed number:') except: print ('input error Please enter a valid number') continue if guess_number > 10: print ('the number entered is out of range') else: time = datetime.now () log [time] = guess_number if guess_number > nb: print ('\ n is not correct Elif guess_number = = nb: print ('\ nWow, you are really my little worm\ n') break else: print ('\ nNo, you underestimate me\ n') print (\ npublish the correct answer: {nb}\ n') return log
View the data entered by the Bureau
Def find_log (log): print ('the valid data entered by the Bureau is:\ n') if len (log): for xrecoy in log.items (): print (f' {x}: {y}') else: print ('empty')
Main program
If _ _ name__ = ='_ _ main__': choose_number = ['1' _ main__': choose_number ='1' '3'] log = {} while True: nb = random_number () intruduce () choose = input (' enter the serial number of the corresponding function:') if choose = = '1seam: log = guess_number (nb) if choose = =' 2seam: find_log (log) if choose = = '3seam: break If choose not in choose_number: print ('Please enter a valid number!') At this point, I believe you have a deeper understanding of "what is the method of Java to realize the number guessing game". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!