current position:Home>Guess numbers and write programs in C + +

Guess numbers and write programs in C + +

2022-02-02 20:58:53 CSDN Q & A

  1. Guess the number -
    Xiao Ming competes with the computer , The computer randomly gives a number (1-100), The number of times Xiao Ming guessed right is 3 Less than ( contain 3 Time ), Xiao Ming wins , Otherwise the computer will win .



Refer to the answer 1:
#include<stdio.h>#include<stdlib.h>int main(){    int num = rand() % 100 + 1;    int n;    for (int i = 0; i < 3; i++)    {        scanf("%d",&n);        if (n==num)        {            printf(" Xiao Ming wins ");            break;        }    }    if (n != num)    {        printf(" Computers win ");    }    return 0;}



Refer to the answer 2:



Refer to the answer 3:
#include <iostream>using namespace std;int main(){    int y = 0, n = 0, i = 0;    while(1)    {        int xm;        int num = rand() % 100 + 1;        cout << " Please input :";        cin >> xm;        if(xm == num)        {            y++;            cout << " Xiao Ming guessed right ." << endl;        }        else        {            n++;            cout << " Xiao Ming guessed wrong ." << endl;        }        if(y >= 3)        {            cout << " Xiao Ming wins ." << endl;            break;        }        if(n >= 3)        {            cout << " The computer wins ." << endl;            break;        }    }    return 0;}



Refer to the answer 4:



Refer to the answer 5:

img




Refer to the answer 6:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202022058515626.html

Random recommended