查看單個文章
  #25  
舊 2014-02-17, 05:35 PM
哈啦 的頭像
哈啦 哈啦 目前離線
論壇管理員
 
註冊日期: 2002-05-28
文章: 23,016
預設

再請教一個問題,這是一個陣列的程式。就是有五個server以及各server的user有多少人?如今輸入一個server的號碼,就去列出該server的user是多少人。如果輸入的server不在其中就回答not listed。
引用:
#include <stdio.h>

int main(void)
{

int serverusers[5][2]=
{
1,14,
2,28,
3,19,
4,8,
5,15};

int server;
int i;

printf("enter the server number: \n");
scanf("%d",&server);

//look it up in the table.
for(i=0;i<5;i++)
if(server==serverusers[i][0]){
printf("there are %d users on server %d.\n",serverusers[i][1],server);
break;
}
//report error if not found.
if(i==5) printf("server not listed.\n");
return 0;
}
為何最後以i等於5的話來判定輸入的值不在預設之中?

thanks
__________________
咖啡走路
微博


您是網站站長嗎?歡迎到站長俱樂部 一起討論吧。
按我看版規
code.club
回覆時引用此篇文章