? asked in 電腦與網際網路程式設計 · 1 decade ago

while迴圈

使用C語言的while迴圈由電腦自動產生1-100數值:

1猜對的話顯示"你猜對了"並結束程式

2猜太大了顯示"你猜太大了"繼續猜

3猜太小了顯示"你猜太小了"繼續猜

重要:老師有規定

.........................................

... 必須用 int r;

... r=rand()%+1;

.... 這樣來寫

............................................

麻煩其他大大幫一下

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    連改字都不會~~考慮一下~明來再來~

    #include <iostream>

    #include <time.h>

    using namespace std;

    int main(){

    int i=0,n,p;

    bool correct=false;

    char* mess[]={"過低","過高"};

    srand(time(0));

    p=rand()%20+1;

    while (i++<5 && !correct)

    {

    cout << "請輸入數字1~20" << i <<"/5 :"<< endl;

    cin >> n;

    if (n == p) correct=true; else cout << mess[n>p] <<endl;

    }

    if (correct) cout << "猜對了"; else cout << "笨死了";

    system("PAUSE");

    return 0;}

Still have questions? Get your answers by asking now.