Use Java and C language to implement the number guessing game (numbers are randomly generated)

Table of Contents

1. Java implementation

1. Content introduction

2. Ideas

3. Complete code

2. C language implementation

1. Ideas

2. Complete code

3. Summary

1. Generate random numbers

2. Branch structure


1. Java implementation

1. Content introduction

(1) After the program runs, we are reminded to enter a number. The guessed number is controlled between [0-100]. The following is the rendering:

(2) You can add some features yourself, such as controlling the number of guesses, or displaying a range of numbers every time you guess wrong

2. Idea

(1) Let the computer generate random numbers by itself

  • How to write random numbers: Run the program once and only need to generate one
import java.util.Random;//The class package that needs to be included (similar to the C language header file)
     Random random = new Random();//Requires tools, where random is the name of the tool defined by yourself
     int number =random.nextInt(100) + 1;//number is used to receive the generated random number
//Input 100, the control range is [0,100), and then + 1 is [0,100]
  • Enter the guessed number from the keyboard
import java.util.Scanner;//Contained class packages
     Scanner scanner = new Scanner(System.in);//Tool, written outside the loop
  • Within the loop, if you don’t guess it, you need to re-enter it.
System.out.print("Please enter the number you want to guess:");
int input = scanner.nextInt();

(2) There are three results for guessing numbers: guessing right, guessing small, and guessing big. One of them will be displayed for each input

  • Use the if else branch structure to control the direction of the results
 System.out.print("Please enter the number you want to guess:");
                int input = scanner.nextInt();
                if(input<number) {
                    System.out.println("Guess too small");
                } else if(input>number) {
                    System.out.println("Guessed it");
                } else {
                    System.out.println("Congratulations on guessing correctly");
                    break;
                }

(3) Control times

  • Just add a variable to control the number of times
 int times = 6;

            while(times>0) {
                System.out.print("Please enter the number you want to guess:");
                int input = scanner.nextInt();
                if(input<number) {
                    times--;
                    System.out.println("Guess is too low, you still have times left" + times);

                } else if(input>number) {
                    times--;
                    System.out.println("Guessed too much, you still have times left" + times);

                } else {
                    System.out.println("Congratulations on guessing correctly");
                    break;
                }
            }
            if(times==0) {
                System.out.println("Fool, no chance, let's live in the next life");
            }

3. Complete code

import java.util.Random;
import java.util.Scanner;
public class Game {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //Generate random numbers
        Random random = new Random();//Tools
        int number =random.nextInt(100) + 1;
        //Only give 6 chances
        int times = 6;

            while(times>0) {
                System.out.print("Please enter the number you want to guess:");
                int input = scanner.nextInt();
                if(input<number) {
                    times--;
                    System.out.println("Guess is too low, you still have times left" + times);

                } else if(input>number) {
                    times--;
                    System.out.println("Guessed too much, you still have times left" + times);

                } else {
                    System.out.println("Congratulations on guessing correctly");
                    break;
                }
            }
         if(times==0) {
                System.out.println("Fool, no chance, let's live in the next life");
            }
    }
}

2. C language implementation

1. Idea

(1) Generate random numbers

  • The rand() function generates pseudo-random numbers, that is, the numbers generated are the same every time it is run.
  • Adding a timestamp: srand((unsigned int)time(NULL)) can control the number generated each time it is run to be different.
srand((unsigned int)time(NULL));
int number = rand() 0 + 1;
  • Header files that need to be included
#include<time.h>//time function
#include<stdlib.h>//rand function

(2) Branch structure

  • It also needs to be divided into three types of results: guess right, guess small and guess big.
while (1)
{
printf("Please enter the number you want to guess:");
scanf("%d", & amp;input);
if (input > number) {
printf("Guess it's big\\
");
}
else if (input < number) {
printf("Guess it's too small\\
");

}
else {
printf("Guessed right: %d\\
",input);
break;
}

(3) Color addition

  • You can set the number of guessing games
int count = 6;
while(count)
{
printf("Please enter the number you want to guess:");
scanf("%d", & amp;input);
if (input > number) {
count--;
printf("Guessed too much, %d times left\\
", count);
\t\t
}
else if (input < number) {
count--;
printf("Guessed too low, %d times left\\
", count);
\t\t\t
}
else {
printf("Guessed right: %d\\
",input);
break;
}
}

  • Simple menu can be set up
void menu()
{
printf("************************\\
");
printf("****** 1.play *****\\
");
printf("****** 0.eixt *****\\
");
printf("************************\\
");
}

int input = 0;
menu();
printf("Enter your choice:");
scanf("%d", & amp;input);

2. Complete code

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void menu()
{
printf("************************\\
");
printf("****** 1.play *****\\
");
printf("****** 0.eixt *****\\
");
printf("************************\\
");
}
int main() {

int input = 0;
srand((unsigned int)time(NULL));
int number = rand() % 100 + 1;
menu();
printf("Enter your choice:");
scanf("%d", & amp;input);
while(input){
int count = 6;
while(count)
{
printf("Please enter the number you want to guess:");
scanf("%d", & amp;input);
if (input > number) {
count--;
printf("Guessed too much, %d times left\\
", count);
}
else if (input < number) {
count--;
printf("Guessed too low, %d times left\\
", count);
}
else {
printf("Guessed right: %d\\
", input);
break;
}
}
}
return 0;
}

3. Summary

1. Generate random numbers

(1) C language requires two steps to generate random numbers:

  • Timestamp needs to be set: srand((unsigned int)time(NULL));
  • Use a function to generate random numbers: int number = rand() % 100 + 1;

(2) Java requires two steps to generate random numbers:

  • Using the toolkit: Random random = new Random();
  • Use the method to generate a random number: int number =random.nextInt(100) + 1;

2. Branch structure

  • In the implementation process of C language and Java, the writing methods of the three directions are very similar, so the ideas between them are similar.
  • In addition, you can add some interesting code yourself to make it richer

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. C Skill Tree Home Page Overview 194,200 people are learning the system