鱼C论坛

 找回密码
 立即注册
查看: 2625|回复: 1

if...if和else if

[复制链接]
发表于 2020-5-31 09:13:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

  1. #include <stdio.h>
  2. #include "windows.h"
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. //定义全局变量
  6. int high,width;//画面尺寸
  7. int bird_x,bird_y;//小鸟坐标
  8. int ball_vx,ball_vy;//小球速度
  9. int bar_y,bar_xdown,bar_xtop;//柱子


  10. void gotoxy(int x,int y)//将光标移动到(x,y)位置
  11. {
  12. HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
  13. COORD pos;
  14. pos.X=x;
  15. pos.Y=y;
  16. SetConsoleCursorPosition(handle,pos);
  17. }

  18. void HideCursor()//隐藏光标
  19. {
  20. CONSOLE_CURSOR_INFO cursor_info={1,0};//第二个值为0表示隐藏光标
  21. SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
  22. }

  23. void startup()
  24. {
  25.         high=15,width=25;
  26.         bird_x=0,bird_y=width/3;
  27.         bar_y=width,bar_xtop=high/4;
  28.         bar_xdown=high/2;
  29.        
  30.        
  31.         HideCursor();
  32. }

  33. void show()
  34. {
  35.         gotoxy(0,0);//光标必须(0,0)
  36.         //system("cls");
  37.         int i,j;
  38.         for(i=0;i<=high+1;i++){
  39.                 for(j=0;j<=width+1;j++){
  40.                         if(i==high+1)//显示下边框
  41.                         {
  42.                                 printf("=");
  43.                         }
  44.                         else if(j==width+1)//显示右边框
  45.                         {
  46.                                 printf("||");
  47.                         }
  48.                         else if((i==bird_x)&&(j==bird_y)) printf("@"); //显示小鸟
  49.                         if((j==bar_y)&&((i<=bar_xtop)||(i>=bar_xdown)))
  50.                         {
  51.                                 printf("*");//输出障碍
  52.                         }               
  53.                         else printf(" ");
  54.                 }
  55.                 printf("\n");
  56.                
  57.         }

  58. }

  59. void update_without()
  60. {       
  61.         bird_x++;
  62.         Sleep(50);
  63.       
  64.          
  65. }
  66. void update_with()
  67. {
  68.         char input;
  69.         if(kbhit()){
  70.                 input =getch();
  71.                 if(input ==' ')
  72.                 {
  73.                         bird_x-=2;
  74.                        
  75.                 }
  76.                
  77.         }
  78.                
  79. }


  80. int main()
  81. {
  82.         system("title 反弹球消砖块");
  83.         //gotoxy(0,0);//把光标移至画面左顶点(0,0)处
  84.         startup();
  85.         //循环,游戏运行
  86.         while(1)
  87.         {
  88.         show();
  89.         update_without();
  90.         update_with();       
  91.        
  92.          }
  93.        
  94.         return 0;
  95. }


复制代码


问题:
show函数里,障碍物*,画面||,会随着小鸟@的下落一起显示
  1.                         else if((i==bird_x)&&(j==bird_y)) printf("@"); //显示小鸟
  2.                         if((j==bar_y)&&((i<=bar_xtop)||(i>=bar_xdown)))//if之前没有加else 造成的,if...if,和else if有什么不一样?求解释。
  3.                         {
  4.                                 printf("*");//输出障碍
  5.                         }               
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-1 09:56:36 | 显示全部楼层
如果写成if...if ,每个if都会判断,if...else if 的话,如果走了if,不会再判断else if,多层的话走了任意一个else if,后面的else if 都不会再判断了。
比如:
  1. #include<stdio.h>
  2. int main(){
  3.         int a = 0;
  4.         int b = 1;
  5.         int c = 2;

  6.         printf("*********case1***********\n");
  7.         //下面这段代码只会输出 a<b
  8.         //多层else if 只要走了一个分支 ,其他的分支不再判断
  9.         if(a>b){
  10.                 printf("a>b\n");
  11.         }else if (a < b){
  12.                 printf("a<b\n");
  13.         }else if (a < c){
  14.                 printf("a<c\n");
  15.         }

  16.         printf("*********case2***********\n");

  17.         //下面这段代码会输出 a<b a<c
  18.     //第二个if 是独立判断的
  19.         if(a>b){
  20.                 printf("a>b\n");
  21.         }else if (a < b){
  22.                 printf("a<b\n");
  23.         }
  24.         if (a < c){
  25.                 printf("a<c\n");
  26.         }




  27.         return 0;
  28. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-16 15:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表