注册 登录
编程论坛 C++教室

请问这个程式不用got該怎麽修改呢?

ts02147823 发布于 2012-12-28 00:59, 325 次点击
#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

int main()
{
    int x,y;
    srand(time(NULL));
    x=(rand()%(100-1+1)+1);

    label:
    cout<<"請輸入一個數字: ";
    cin >>y;  

    if(y == x)
    cout <<"答對了"<<endl;


    if(y > x)
    {
    cout <<"太大了"<<endl;
    goto label;
    }

    if(y < x)
    {
    cout <<"太小了"<<endl;
    goto label;
    }


    system("PAUSE");
    return 0;
}

我只會用goto,請大神指點一下

謝謝!
5 回复
#2
StarFall2012-12-28 08:24
#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

int main()
{
int x,y;
srand(time(NULL));
x=(rand()%(100-1+1)+1);

while(1){
cout<<"請輸入一個數字: ";
cin >>y;

if(y == x)
{cout <<"答對了"<<endl;
break;}

if(y > x)
{
cout <<"太大了"<<endl;

}

if(y < x)
{
cout <<"太小了"<<endl;
}

}
system("PAUSE");
return 0;
}
手机改的  运行通过 。 用一个循环套住就可以了
#3
ts021478232012-12-28 17:51
以下是引用StarFall在2012-12-28 08:24:38的发言:

#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

int main()
{
int x,y;
srand(time(NULL));
x=(rand()%(100-1+1)+1);

while(1){
cout<<"請輸入一個數字: ";
cin >>y;

if(y == x)
{cout <<"答對了"<<endl;
break;}

if(y > x)
{
cout <<"太大了"<<endl;

}

if(y < x)
{
cout <<"太小了"<<endl;
}

}
system("PAUSE");
return 0;
}
手机改的  运行通过 。 用一个循环套住就可以了


抱歉,请问一下while(1)
里面的1是甚麽意思呢?
#4
wp2319572012-12-28 20:07
while(1)表示永真值   
最简单的死循环设计就是 while(1);
#5
StarFall2012-12-28 23:19
while(1)  就是一直循环直到猜对触发了break;跳出循环。
#6
ts021478232012-12-29 00:26
了解,就是除非迴圈遇到break;
否則就會繼續循環下去,感謝兩位
1