2-28

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    char a;
    while (true)
    {

        cout << "Menu: A(dd) D(elete) S(ort) Q(uit),Select one:";
        cin >> a;
        if (a == 'A')
        {
            cout << "data has been added" << endl;
            continue;
        }
        if (a == 'D')
        {
            cout << "data has been deleted" << endl;
            continue;
        }
        if (a == 'S')
        {
            cout << "data has been sorted" << endl;
            continue;
        }
        if (a == 'Q')
        {

            break;
        }
        else
        {
            cout << "no such choice concluded" << endl;
            continue;
        }
    }
    return 0;
}

2-29

#include<iostream>

using namespace std;
int main() {
    int i, j;
    for (i = 2; i < 100; i++)
    {
        for (j = 2; j <= i; j++)

            if (i%j == 0)
    }
    if (i == j) cout << i << endl;


    return 0;
}

改用do while结构,则中间循环代码为

do
    {
        for(j=2;j<=i;j++)
        {
            if(i%j==0)break;
        }
        if(j==i)cout<<i<<endl;
        i++;
    }while(i<=100);

while循环

while(i<=100)
    {
        for(j=2;j<=i;j++)
        {
            if(i%j==0)break;
        }
        if(j==i)cout<<i<<endl;
        i++;
    }

2-32

猜数游戏,这里我们猜固定的一个数

#include<iostream>

using namespace std;
int main()
{
    int a=50,b;
    while(true)
    {
    
    cin>>b;
    if(b==a)
        {
        cout<<"you are right"<<endl;
        return 0;
        }
    if(b>a)
        cout<<"it is bigger"<<endl;
    if(b<a)
        cout<<"it is smaller"<<endl;
        
    
    
    }
    return 0;
}

2-34

取球问题,在五种颜色中选出三种颜色

#include <iostream>
using namespace std;
int main()
{
    int a = 5, b = 3, i, n, m, s;
    n = a, m = b, s = b;
    for (i = 1; i < s; i++)
    {
        n--;
        a = a * n;
    }
    for (i = 1; i < s; i++)
    {
        m--;
        b = b * m;
    }
    a = a / b;
    cout << a << endl;
    return 0;
}

评论地址:

https://www.cnblogs.com/charlotte00/p/10546549.html

https://www.cnblogs.com/yidaoyigexiaopenyou/p/10556646.html

https://www.cnblogs.com/hongzai1206/p/10548435.html