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

[开动脑筋]大家来尽情发挥,简单程序

zptk720 发布于 2007-09-02 13:01, 2583 次点击
编写一个完整的程序,实现功能:向用户提问“现在正在下雨吗?”,提示用户输入Y或N。若输入为Y,显示“现在正在下雨。”;若输入为N,显示“现在没有下雨。”;否则继续提问“现在正在下雨吗?”。

上面是题目,大家发挥吧。
31 回复
#2
aipb20072007-09-02 13:11
彻底无语!
#3
valentineyzq2007-09-02 15:13
不好玩啊。也只有我这么无聊的人才会回帖。
#4
woshibuhui2007-09-02 16:40

#include <iostream.h>
{
char answer;
while(1)
{
cout<<"现在还下雨吗?(Y/N)"<<endl;
cin>>answer;
if(answer=='Y'||answer=='y')
cout<<"现在正在下雨。"<<endl;
elseif(answer=='N'||answer=='n')
{
cout<<"现在没有下雨。"<<endl;
break;
}
else
cout<<"错误的输入!(Y/N)"<<endl;
}
}

#5
woshibuhui2007-09-02 16:42
#include <iostream.h>
void main()
{
char answer;
while(1)
{
cout<<"现在还下雨吗?(Y/N)"<<endl;
cin>>answer;
if(answer=='Y'||answer=='y')
cout<<"现在正在下雨。"<<endl;
elseif(answer=='N'||answer=='n')
{
cout<<"现在没有下雨。"<<endl;
break;
}
else
cout<<"错误的输入!(Y/N)"<<endl;
}
}
#6
zptk7202007-09-02 18:31
回复:(woshibuhui)#include { ...

貌似不对!~
#7
zptk7202007-09-02 18:32
以下是引用woshibuhui在2007-9-2 16:42:46的发言:
#include <iostream.h>
void main()
{
char answer;
while(1)
{
cout<<"现在还下雨吗?(Y/N)"<<endl;
cin>>answer;
if(answer=='Y'||answer=='y')
cout<<"现在正在下雨。"<<endl;
elseif(answer=='N'||answer=='n')
{
cout<<"现在没有下雨。"<<endl;
break;
}
else
cout<<"错误的输入!(Y/N)"<<endl;
}
}

这个也不对

#8
不问不学2007-09-02 19:54
加个循环!!!!!!
再重构代码
#9
福尔摩斯2007-09-02 20:53
#include <iostream>
using namespace std;
int main()
{
char answer;
here:cout<<"现在还下雨吗?(Y/N)"<<endl;
cin>>answer;
if(answer=='Y'||answer=='y')
cout<<"现在正在下雨。"<<endl;
else if(answer=='N'||answer=='n')
cout<<"现在没有下雨。"<<endl;
else goto here;
cin.get();
cin.get();
return 0;
}

这个时候用goto语句才是省内存的
#10
绝种好小伙2007-09-03 10:36
不错,有点水平
#11
圆圆的鸟蛋2007-09-03 12:30
这个有什么意思吗??
#12
wsy2007-09-03 12:36
有点体会 谢谢
#13
virusswb2007-09-03 16:48
楼主的用心值的怀疑
#14
xlh52252007-09-03 20:08
好无语哦~~~
#15
penganlph2007-09-03 20:59
9楼的要好一些。
#16
qwl19822007-09-04 02:04
楼主的意思是,这道题是YY题,大家自由发挥。
比如说用类啦,重载啦,流啦,指针什么的解决
#17
qwl19822007-09-04 02:05
不求最快,但求最复杂
#18
從霝開匙2007-09-04 16:54
[QUOTE][/QUOTE]
以下是引用woshibuhui在2007-9-2 16:42:46的发言:
#include <iostream.h>
void main()
{
char answer;
while(1)
{
cout<<"现在还下雨吗?(Y/N)"<<endl;
cin>>answer;
if(answer=='Y'||answer=='y')
cout<<"现在正在下雨。"<<endl;
elseif(answer=='N'||answer=='n')
{
cout<<"现在没有下雨。"<<endl;
break;
}
else
cout<<"错误的输入!(Y/N)"<<endl;
}
}




把elseif分开 else if
#19
snakeImao2007-09-15 20:48
好象不推荐使用goto语句哦!!!
#20
Adicelanton2007-09-15 20:51
一个do {...}while(..);搞定.
...............
#21
squallssck2007-09-16 00:56

using System;


namespace ifRain
{
class Program
{
static void Main(string[] args)
{

do
{
Console.Write("今天下雨嘛??()Y/N");

string str = Console.ReadLine();

if (str == "Y" | str == "y") { Console.WriteLine("今天下雨!!!"); }

else if (str == "N" | str == "n") { Console.WriteLine("今天不下雨"); }

else { Console.WriteLine("找茬????"); break; }


} while (true);


}
}
}

#22
孤魂居士2007-09-18 16:27
怎么到处都有
#23
niewucai2007-09-19 23:02
goto语句现在基本没人用了。
#24
dwarf_ren2007-09-20 00:26
#include <iostream.h>
void main()
{
char answer;
cout<<"现在还下雨吗?(Y/N)"<<endl;
cin>>answer;
if(answer=='y')
{
cout<<"现在下雨"<<endl;
}
else
{
if(answer=='n')
{
cout<<"现在不下雨"<<endl;
}
else
{
cout<<"go out"<<endl;
}
}
}

真有意思
#25
dlgdd2007-09-20 09:05
#include<iostream>
#include<string>
using namespace std;
bool lnJudge()
{
char answer;
cout<<"现在还在下雨吗?(Y/N)"<<endl;
cin>>answer;
if(answer=='Y'||answer=='y')
{
cout<<"现在还在下雨!"<<endl;
return true;
}
elseif(answer=='N'||answer=='n')
{
cout<<"现在没有下雨"<<endl;
return true;
}
else return false;

}

//----------------------------------------
int main()
{
while(!lnJudge())
continue;
getchar();
return 0;
}

//小弟不才,现丑了

[此贴子已经被作者于2007-9-20 9:11:02编辑过]

#26
duxinjun1232007-09-20 16:55
#27
王振兴2007-10-12 20:28
太无聊了!
#28
永夜的极光2007-10-13 07:30
谁用C++内嵌汇编写一个把。。。
#29
wdtk2007-10-14 10:57

没事做,回一个了!!
#include<iostream>
#include<string>
using namespace std;

int main()
{
string str;
while(1)
{
cout<<"现在下雨吗?[Y][N]"<<endl;
cin>>str;
do
{

if(str=="Y")
{
cout<<"现在正在下雨!!\n"<<endl;
break;
}
if(str=="N")
{
cout<<"现在没有下雨!!\n"<<endl;
break;
}
}
while(str=="Y"||str=="N");
}
return 0;
}

[此贴子已经被作者于2007-10-14 10:58:38编辑过]

#30
chris2232007-10-15 04:46

同意楼上,我也认为do while是做这个问题最好,最简单的选择,其他的都要么是杀机用牛刀,要么就是有点啰嗦。

#31
凉拌粉丝2007-10-15 19:49
#include <iostream>
using namespace std;
void main()
{
char answer;
while(1)
{
cout<<"现在还下雨吗?(Y/N)"<<endl;
cin>>answer;
if(answer=='Y'||answer=='y')
{ cout<<"现在正在下雨。"<<endl;
break;
}
else if(answer=='N'||answer=='n')
{
cout<<"现在没有下雨。"<<endl;
break;
}

}
}
楼主这样就可以了,不需要用goto 语句的。我已经调试通过了。

#32
dart2007-10-16 13:43
#include<iostream>
using namespace std;
int main()
{
char str;
step:cout<<"现在下雨吗?"<<endl;
cin>>str;
if(str!='Y'||str!='y'||str!='n'||str!='N')
goto step;
else if(str=='Y'||str=='y')
cout<<"现在在下雨"<<endl;
else
cout<<"现在没有下雨"<<endl;

goto step;
return 0;
}
1