一.分支语句
if (n = 5) //n=5 为真
{
//会执行
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[]) {
//石头剪刀布 游戏
int b =arc4random_uniform(3);
printf("计算机输入的是 %d\n",b);
printf("请输入一个数 0.石头 1.剪刀 2.布 :");
int a = -1 ;
scanf("%d",&a);
if (a > 2 || a<0 ) {
printf("输入不符合规则,请重新输入!\n");
}
else if ((b == 1 && a==2 ) || (b == 2 && a==0)||(b== 0 && a==1)) {
printf("电脑胜利 a = %d ,b = %d \n",a,b);
}
else if((a == 1 && a+b==3) || (a == 2 && a+b ==2)||(a== 0 && a+b ==1))
{
printf("玩家胜利 a = %d ,b = %d \n",a,b);
}
else {
printf("平手 a = %d ,b = %d\n",a,b);
}
return 0;
}
2.if注意点
1).If(条件); //表示什么也不干
2).;空语句
3).switch 的 case注意穿透问题
4).在语句后加 break;结束switch语句
5).应用:头等舱问题
#include <stdio.h>
int main(int argc, const char * argv[]) {
int num = 0,money=0;
printf("请输入一个number :");
scanf("%d",&num);
switch (num) {
case 37:
money+=50;
break;
case 65:
printf("头等舱\n");
money+=80;
case 12:
money+=20;
break;
default:
break;
}
printf("money = %d ,num = %d\n",money,num);
return 0;
}
6).成绩显示代码
#include <stdio.h>
int main(int argc, const char * argv[]) {
printf("请输入成绩:");
int score = 0 ;
scanf("%d",&score);
if (score > 100 || score < 0) {
printf("成绩输入不合法,请重新输入");
}
else{
switch (score/10) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
printf("成绩为 E \n");
break;
case 6:
printf("成绩为 D \n");
break;
case 7:
printf("成绩为 C \n");
break;
case 8:
printf("成绩为 B \n");
break;
case 9:
case 10:
printf("成绩为 A \n");
break;
default:
printf("ohter!");//放在结尾 ,可以不加break //放在开头需要加
break;
}
}
return 0;
}
7). 判断季节的问题
#include <stdio.h>
int main(int argc, const char * argv[]) {
printf("输入一个月份:");
int season = 0 ;
scanf("%d",&season);
if (season > 12 || season < 1) {
printf("输入的月份不合法,超过范围,请重新输入\n");
}
else{
switch (season) {
case 12:
case 1:
case 2:
printf("%d 月份属于冬季 \n",season);
break;
case 3:
case 4:
case 5:
printf("%d 月份属于春季 \n",season);
break;
case 6:
case 7:
case 8:
printf("%d 月份属于夏季 \n",season);
break;
case 9:
case 10:
case 11:
printf("%d 月份属于秋季 \n",season);
break;
default:
break;
}
}return 0;
}
8).简单计算机代码:
#include <stdio.h>
int main(int argc, const char * argv[]) {
printf("请输入两个数字:");
int num1 =0 ,num2 =0;
scanf("%d%d",&num1,&num2);
char ch ;
printf("请输入一个运算符:");
scanf("%c%c",&ch,&ch);
switch (ch) {
case '+':
printf("%d %c %d = %d\n",num1,ch,num2,num1+num2);
break;
case '-':
printf("%d %c %d = %d\n",num1,ch,num2,num1-num2);
break;
case '*':
printf("%d %c %d = %d\n",num1,ch,num2,num1*num2);
break;
case '/':
if (num2!=0)
printf("%d %c %d = %f\n",num1,ch,num2,(float)num1/num2);
else
printf("除数不能为0,请重新输入!\n ");
break;
default:
printf("运算符号输入有误!\n");
break;
}
return 0;
}
<p style="margin: 0in; font-family: Menlo; font-size: 9.75pt;"></p><p style="margin: 0in; font-family: Menlo;"><span style="font-size:32px;"><span lang="en-US" style="white-space: pre; font-family: Calibri;">三.While(</span><span lang="zh-CN" style="white-space: pre; font-family: SimSun;">条件</span><span lang="en-US" style="white-space: pre; font-family: Calibri;">)</span><span lang="zh-CN" style="white-space: pre; font-family: SimSun;">;空语句造成死循环</span></span><span style="font-size:12px;">
</span></p><p style="margin: 0in; font-family: Menlo;">
</p><pre name="code" class="cpp">int main(intargc, const char* argv[]) {
// insertcode here...
int i =2;
while (i);//不会输出 下面的hello world
/*while是当条件为真(就是为1)是执行{}中的指令,而这条指令{}为空,就是什么都不执行,而条件永远为真,所以程序就一直在这里执行,不向下走了。*/
printf("Hello,World!\n");
return 0;
}
<span style="font-size:24px;">2). C5while计算接收字符个数</span>
//
// Created by CHINGWEI_MACPC on 15/10/15.
// Copyright © 2015年 itcast. All rights reserved.
//
#include <stdio.h>
int main(intargc, const char* argv[]) {
int count =0;
char ch ='0';
while (ch != '\n') {
scanf("%c",&ch);
count++;
}
printf("共有 %d 个字符!\n",count);
return 0;
}
</pre><p><span style="font-size:24px;">3).switch练习3</span></p><p style="margin: 0in; font-family: Menlo; font-size: 9.75pt;"></p><pre name="code" class="cpp">#include <stdio.h>
int main(intargc, const char* argv[]) {
char ch ;
int flag = 1;
while (flag) {
scanf("%c",&ch);
switch (ch) {
case 'W':
case 'w':
printf("上!\n");
break;
case 's':
case 'S':
printf("下!\n");
break;
case 'a':
case 'A':
printf("左!\n");
break;
case 'd':
case 'D':
printf("右!\n");
break;
case 'q':
case 'Q':
printf("程序正在退出!\n");
printf("程序已经退出!\n");
flag = 0;
break;
default:
break;
}
}
return 0;
}
4).while练习,疯狂猜数
#include <stdio.h>
#include <stdlib.h>
int main(intargc, const char* argv[]) {
int num = 0;
int computerNum =arc4random_uniform(1000);
printf("%d\n",computerNum);
int flag = 3 ;
printf("猜数开始,请输入【1,1000】数字:");
scanf("%d",&num);
while(flag){
if (num == computerNum) {
printf("猜对了,你真棒!\n");
flag = 0;
}
else{
flag-- ;
if (flag != 0&& num > computerNum) {
printf("竞猜结果:数字过大,你还剩下%d 次机会 :",flag);
printf("\n猜数开始,请输入【1,1000】数字:");
scanf("%d",&num);
}
else if(flag != 0 && computerNum > num )
{
printf("竞猜结果:数字过小,你还剩下 %d 次机会:",flag);
printf("\n猜数开始,请输入【1,1000】数字:");
scanf("%d",&num);
}
else {
printf("猜数结束,你不适合这个游戏!\n");
}
}
}
return 0;
}
arc4random_uniform(100)+10;代表随机数范围是10-110
5).for语句练习,打印三角数字
// C5打印三角数字
//
// Created by CHINGWEI_MACPC on 15/10/16.
// Copyright © 2015年 itcast. All rights reserved.
//
#include <stdio.h>
int main(intargc, const char* argv[]) {
int i =0 ,j=0;
for(i = 1;i <= 5;i++)
{
for (j = 1; j <= i; j++) {
printf("%d\t",j);
}
printf("\n");
}
for (i=5; i>0; i--) {
for (j=1; j<i; j++) {
printf("%d\t",j);
}
printf("\n");
}
return 0;
}
输出样式:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Program ended with exit code: 0
6).for语句练习,打印正三角形
// main.c
// C5正三角形
//
// Created by CHINGWEI_MACPC on 15/10/16.
// Copyright © 2015年 itcast. All rights reserved.
//
#include <stdio.h>
int main(intargc, const char* argv[]) {
int i ,j;
for(i = 0;i <= 6;i++)
{
for(j = 0 ;j<6-i;j++){
printf(" ");
}
for (j =i; j <= i*2-1; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
<span style="font-family: Menlo; font-size: 8.25pt; background-color: rgb(255, 255, 255);"><strong> </strong></span>
*
* *
* * *
* * * *
* * * * *
* * * * * *
Program ended with exit code: 0
7).for语句练习,打印正三角形
#include <stdio.h>
int main(intargc, const char* argv[]) {
int i ,j;
for(i = 1;i <= 6;i++)
{
for(j = 0 ;j<=6-i;j++){
printf(" ");
}
for (intk =0; k <i*2-1; k++) {
printf("*");
}
printf("\n");
}
return 0;
}<span style="font-family: Menlo; font-size: 9.75pt; background-color: rgb(255, 255, 255);"> </span>
*
***
*****
*******
*********
***********
Program ended with exit code: 0
8).for语句练习,打印出乘法表
#include <stdio.h>
int main(intargc, const char* argv[]) {
int i =1 , j =1 ;
for (i=1; i<=9; i++) {
for (j=1; j<=i; j++) {
printf("%d * %d = %d ",i,j,i*j);
}
printf("\n");
}
return 0;
}
补充:安装VVDocumenter插件
1).Github
打开项目,编译,然后关闭掉xcode
/Users/chingwei_macpc/Library/ApplicationSupport/Developer/Shared/Xcode/Plug-ins
Github 国外源代码托管平台
/Users/chingwei_macpc/Library/ApplicationSupport/Developer/Shared/Xcode/Plug-ins
2)打开项目,编译,然后关闭掉xcode,快捷键///连续输入三个/
补充2:Main函数理解