Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ªì¾Ç ½Ð±Ð¤@­Ó°ÝÃD

0 views
Skip to first unread message

lead my life

unread,
Apr 20, 2001, 2:49:51 AM4/20/01
to
#include<stdio.h>
#include<conio.h>

main()
{
char ch;
int num;
printf("please enter a positive interger\n");
do{
ch=getch();
}while(ch<'0'|| ch>'9');
num=0;
do{
num=num*10+ch-'0';
ch=getch();
}while(ch>='0' && ch<='9');
printf("the number is %d\n",num);
}
我們老師說這個程式會把你輸入的字元(0-9)轉換成數字
例如 輸入字元1 字元2 字元3 字元A
他就會顯現123這個數字
我回家看一次之後有一個問題
就是------第一個do {}while 他的功能是做什麼的
少了他不行嗎
可否幫我解答一下 謝謝

--
◎ [1;31m龍 [32m貓 [33m資 [34m訊 [35m天 [36m地 [0m( [1mbbs.mgt.ncu.edu.tw [0m)
◎[ [1;33;46missn [0m]From: i102-35.cc.ncu.edu.tw

剪短了長髮

unread,
Apr 20, 2001, 8:10:27 AM4/20/01
to
【 在 issn...@bbs.mgt.ncu.edu.tw (lead my life) 的大作中提到: 】
: #include<stdio.h>

: #include<conio.h>
: main()
: {
: char ch;
: int num;
: printf("please enter a positive interger\n");
: do{
: ch=getch();
: }while(ch<'0'|| ch>'9');
: num=0;
: do{
: num=num*10+ch-'0';
: ch=getch();
: }while(ch>='0' && ch<='9');
: printf("the number is %d\n",num);
: }

do {} while();
是說你程式執行到此處時,第一次會直接跑do {}內的敘述,此時已指定一個值給 ch ,
然後才執行 while () ,若ch的值不滿足while() 內的條件,則會跳回do那行。

也就是說 do {} while() 和 while() 的不同在於程式第一次執行到這行的時候,
do while 不需滿足任何條件 ,while 這指令則要滿足條件方能執行內里的敘述。


: 我們老師說這個程式會把你輸入的字元(0-9)轉換成數字


: 例如 輸入字元1 字元2 字元3 字元A
: 他就會顯現123這個數字
: 我回家看一次之後有一個問題
: 就是------第一個do {}while 他的功能是做什麼的
: 少了他不行嗎
: 可否幫我解答一下 謝謝


--

可惜
我們的故鄉
容不下我們的理想

[m [1;35m※ 來源:‧大紅花的國度 MSIA.pine.ncu.edu.tw‧[FROM: 140.112.250.49] [m

0 new messages