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. Ideas

(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;//需要包含的类包(类似C语言的头文件)
     Random random = new Random();//需要工具,其中random是自己定义的工具名
     int number =random.nextInt(100)+1;//number用来接收产生的随机数
//输入100则是控制范围为[0,100),我们再+1即是[0,100]
  • Enter the guessed number from the keyboard
import java.util.Scanner;//包含的类包
     Scanner scanner = new Scanner(System.in);//工具,写在循环外
  • Within the loop, if you don’t guess it, you need to re-enter it. 
System.out.print("请输入你要猜的数字:");
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 every time you enter.

  • Use the if else branch structure to control the direction of the results. 
 System.out.print("请输入你要猜的数字:");
                int input = scanner.nextInt();
                if(input<number) {                  
                    System.out.println("猜小了");
                } else if(input>number) {               
                    System.out.println("猜大了");
                } else {
                    System.out.println("恭喜你猜对了");
                    break;
                }

(3) Control times

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

            while(times>0) {
                System.out.print("请输入你要猜的数字:");
                int input = scanner.nextInt();
                if(input<number) {
                    times--;
                    System.out.println("猜小了,你的次数还剩"+times);

                } else if(input>number) {
                    times--;
                    System.out.println("猜大了,你的次数还剩"+times);

                } else {
                    System.out.println("恭喜你猜对了");
                    break;
                }
            }
            if(times==0) {
                System.out.println("傻瓜,没机会了,下辈子吧");
            }

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);
        //产生随机数
        Random random = new Random();//工具
        int number =random.nextInt(100)+1;
        //只给6次机会
        int times = 6;

            while(times>0) {
                System.out.print("请输入你要猜的数字:");
                int input = scanner.nextInt();
                if(input<number) {
                    times--;
                    System.out.println("猜小了,你的次数还剩"+times);

                } else if(input>number) {
                    times--;
                    System.out.println("猜大了,你的次数还剩"+times);

                } else {
                    System.out.println("恭喜你猜对了");
                    break;
                }
            }
         if(times==0) {
                System.out.println("傻瓜,没机会了,下辈子吧");
            }
    }
}

2. C language implementation

1. Ideas

(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()%100+1;
  • Header files that need to be included
#include<time.h>//time函数
#include<stdlib.h>//rand函数

(2) Branch structure

  • It also needs to be divided into three results, guess right, guess small and guess big.
while (1)
	{
		printf("请输入你要猜的数字:");
		scanf("%d", &input);
		if (input > number) {
			printf("猜大了\n");
		}
		else if (input < number) {
			printf("猜小了\n");

		}
		else {
			printf("猜对了:%d\n",input);
			break;
		}

(3) Color addition

  • You can set the number of guessing games
int count = 6;
	while (count)
	{
		printf("请输入你要猜的数字:");
		scanf("%d", &input);
		if (input > number) {
			count--;
			printf("猜大了,还剩%d次\n",count);
		
		}
		else if (input < number) {
			count--;
			printf("猜小了,还剩%d次\n",count);
			
		}
		else {
			printf("猜对了:%d\n",input);
			break;
		}
	}

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

int input = 0;
menu();
	printf("输入你的选择:");
	scanf("%d", &input);

2. Complete code

#define _CRT_SECURE_NO_WARNINGS 1

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

	int input = 0;
	srand((unsigned int)time(NULL));
	int number = rand() % 100 + 1;
	menu();
	printf("输入你的选择:");
	scanf("%d", &input);
	while(input){
	int count = 6;
	while (count)
	{
		printf("请输入你要猜的数字:");
		scanf("%d", &input);
		if (input > number) {
			count--;
			printf("猜大了,还剩%d次\n", count);
		}
		else if (input < number) {
			count--;
			printf("猜小了,还剩%d次\n", count);
		}
		else {
			printf("猜对了:%d\n", input);
			break;
		}
	}
}
	return 0;
}

3. Summary

1. Generate random numbers

(1) Generating random numbers in C language requires two steps:

  • Need to set the timestamp: 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 method to generate random numbers: 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

Guess you like

Origin blog.csdn.net/2301_77053417/article/details/134105597