目录
1.sleep分析
sleep,可以让程序暂停几秒钟,结构为:
sleep(int)
sleep的头文件在不适用万能头(#include<bits/stdc++.h>)时是:
#include <unistd.h>
/*可以在https://c.runoob.com/compile/12/(在线编译器,有多种语言,例如C++、Python、C#、Go等)、Linux、https://www.json.cn/runcode/run_cpp/(与c.runnoob相似)等运行*/
//或者用
#include <windows.h>
/*可以在Code::blocks、DEV-C++等运行*/
2.while与do-while的区别
while在不符合条件时不会运行,而do-while在不符合条件时会运行1次。
while写法如下:
#include<iostream>
using namespace std;
int main()
{
int cnt = 0,n;
char ch = 'A';
cin >> n;
while(n > 2)
{
if(ch >= 'Z')
{
ch = 'A';
}else if(cnt % 6 == 0 and cnt != 0)
{
cout << endl;
}else
{
if(cnt != 0)
{
cout << " ";
}
}
cout << ch;
ch ++;
cnt ++;
n --;
}
return 0;
}
输入:
2
输出:
do-while代码如下:
#include<iostream>
using namespace std;
int main()
{
int cnt = 0,n;
char ch = 'A';
cin >> n;
do{
if(ch >= 'Z')
{
ch = 'A';
}else if(cnt % 6 == 0 and cnt != 0)
{
cout << endl;
}else
{
if(cnt != 0)
{
cout << " ";
}
}
cout << ch;
ch ++;
cnt ++;
n --;
}while(n > 2);
return 0;
}
输入:
2
输出:
A
3.char数组
char与char数组输出输入的区别:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char a;
char b[25];//我们在用数组的时候经常会把用量+5,避免用量不足,所以我要输入(出)的是20个字符。
cin >> a;//a的输入。
cout << a;//a的输出。
for(int i = 1;i <= n;i ++)
{
cin >> b[i];//b的输入。
cout << b[i] << " ";//b的输出,为方便理解,这里用了空格来分割b[i]。
}
return 0;
}
4.代码呈现:
来,上代码!!!
#include <iostream>
#include <unistd.h>
using namespace std;
char a[25];
int main(){
cout << "数字游戏\n1.开始游戏\n2.查看规则\n";
int a1,a2;
cin >> a1;
if(a1 == 1)
{
cout << "正在准备中......\n";
sleep(2);
cout << "请输入人数:\n";
cin >> a2;
}else if(a1 == 2)
{
cout << "请不要输入7的倍数或含有7的数字,否则就会淘汰。(如果它是7的倍数或含有7的数字,输入0,如果到了350,玩家胜)";
sleep(2);
cout << "loading......\n";
sleep(2);
cout << "请输入游玩人数:\n";
cin >> a2;
}else
{
cout << "丨!";
return 0;
}
for(int i = 1;i <= a2;i ++)
{
a[i] = 'l';
}
cout << "Your Number Is:";
int b1 = rand() % a2;
sleep(2);
cout << b1;
int a3 = 1,b11,b2,b3;
for(int z = 1;z <= a2; z++)
{
for(int i = 1;i <= a2;i ++)
{
cout << endl << i << "号:" << a[i];
if(i == 6)
{
cout << endl;
}else
{
cout << " ";
}
}
while(a[b1] not_eq 'd')
{
for(int i = 1;;)
{
cout << i << "号:" ;
if(i == b1)
{
cin >> a3;
}else
{
cout << a3 << endl;
}
b11 = a3 / 100;
b2 = a3 / 10 % 10;
b3 = a3 % 100 % 10;
if(a3 % 7 == 0 or b1 == 7 or b2 == 7 or b3 == 7 and i == b1)
{
a[b1] = 'd';
cout << "game over\n";
return 0;
}else if(a3 % 7 == 0 or b1 == 7 or b2 == 7 or b3 == 7)
{
a[i] = 'd';
}else if(a3 == 300)
{
cout << "You Won!\n";
return 0;
}
}
}
}
return 0;
}