继续输入(Y/N)控制语句的问题

priestmoon 2008-05-21 07:30:46
不知为何,下面的代码似乎不能实现 "继续输入?(Y/N)"的功能。
哪位能分析一下错误原因,并作出适当的修改?

#include <iostream>
using namespace std;

int main()
{
int i,j,sum;
cout<<"Please input two numbers: ";
cin>>i>>j;
sum=i+j;
cout<<i<<" "<<j<<" "<<sum<<endl;
//to display integer i,j and i+j

while(true)
{
cout<<"continue?(Y/N) :"; //press 'Y' if being to continue input.
//otherwise, jump out.
if( !(getchar()=='Y'&&getchar()=='y') )
//There seems to be something wrong with the line above.
break;
cout<<"Please input two numbers: ";
cin>>i>>j;
sum=i+j;
cout<<i<<" "<<j<<" "<<sum<<endl;
}

return 0;
}
...全文
316 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
priestmoon 2008-05-22
  • 打赏
  • 举报
回复

哪位能解释一下错误原因?
zsxcn 2008-05-22
  • 打赏
  • 举报
回复
if( !(getchar()=='Y'&&getchar()=='y') )应该改成
if( !(getchar()=='Y' || brvbargetchar()=='y') )
zsxcn 2008-05-22
  • 打赏
  • 举报
回复
if( !(getchar()=='Y'&&getchar()=='y') )应该改成
if( !(getchar()=='Y'||getchar()=='y') )
MaybeDevil 2008-05-22
  • 打赏
  • 举报
回复
//Test.cpp
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
int i,j,sum;
cout < <"Please input two numbers: "; // show
char ch = 0;

do {

cin>>i>>j;
sum=i+j;
cout < <i < <" " < <j < <" " < <sum < <endl;
//to display integer i,j and i+j
cout << "Do you want to contimue?(y/n)" << endl; // get the result
cin >> ch; // get the entering
cout << endl;
}while(ch == 'y' || ch == 'Y') // check the result ,if equals 'y'and'Y',break ,and continue the next return 0


return 0;
}

pengzhixi 2008-05-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 pengzhixi 的回复:]
改成do....while 试试

C/C++ code
#include <iostream>
using namespace std;

int main()
{
int i,j,sum;
do{
cout < <"Please input two numbers: ";
cin>>i>>j;
sum=i+j;
cout < <i < <" " < <j < <" " < <sum < <endl;
char ch;
cout < <"ch?(Y/N) :";
cin>>continue;
}while(ch=='y'||ch=='Y')
}

return 0;
}
[/Quote]
犯了低级错误,把continue用做变量名了
pengzhixi 2008-05-22
  • 打赏
  • 举报
回复
改成do while 不行?
limingjiu 2008-05-22
  • 打赏
  • 举报
回复
if( !(getchar()=='Y'&&getchar()=='y') )
//There seems to be something wrong with the line above.
break;
一个字符不能同时为'Y'和'y'
priestmoon 2008-05-22
  • 打赏
  • 举报
回复
按照各位的意见改了一下,但好象还是不行...


#include <iostream>
using namespace std;

int main()
{
int i,j,sum;
cout<<"Please input two numbers: ";
cin>>i>>j;
sum=i+j;
cout<<i<<" "<<j<<" " <<sum <<endl;

while(true)
{
cout<<"continue?(Y/N) :"; //press 'Y' if being to continue input.
char ch=getchar();
if( !(ch=='Y' || ch=='y') )
break;
cout<<"Please input two numbers: ";
cin>>i>>j;
sum=i+j;
cout<<i<<" "<<j<<" "<<sum<<endl;
}

//system("pause");
return 0;
}
waterfield 2008-05-22
  • 打赏
  • 举报
回复
逻辑没有分析清楚。
hyram 2008-05-22
  • 打赏
  • 举报
回复
把你的&&改成||否则就是永假式
lifelovers 2008-05-22
  • 打赏
  • 举报
回复
是输入输出的那个符号,模拟搞错了一些,C++明确要求是“<<”或是“>>”,你的有些有空格空开了
pengzhixi 2008-05-21
  • 打赏
  • 举报
回复
改成do....while 试试

#include <iostream>
using namespace std;

int main()
{
int i,j,sum;
do{
cout < <"Please input two numbers: ";
cin>>i>>j;
sum=i+j;
cout < <i < <" " < <j < <" " < <sum < <endl;
char continue;
cout < <"continue?(Y/N) :";
cin>>continue;
}while(continue=='y'||continue=='Y')
}

return 0;
}
内容概要:该文档为一段用于Google Earth Engine(GEE)平台的JavaScript代码脚本,旨在对研究区域(AOI)内的Landsat 8卫星影像进行土地利用/土地覆盖(LULC)分类。脚本首先筛选2024年云量低于10%的Landsat 8 Level-2地表反射率影像,构建中值合成影像并应用缩放因子校正。随后,基于预先定义的水体(Water)和植被(Vegetation)样本点,合并训练样本并提取影像对应波段数据,采用随机森林分类器(smileRandomForest)对影像进行监督分类。最后,通过划分训练集与测试集,输出混淆矩阵、总体分类精度和Kappa系数,完成分类结果的精度评估。; 适合人群:具备遥感图像处理基础知识的科研人员、地理信息系统(GIS)技术人员以及环境监测相关领域的研究生或专业人员;熟悉GEE平台基本操作者更佳; 使用场景及目标:①实现特定区域多光谱遥感影像的土地覆盖分类;②掌握GEE平台上从影像预处理、样本构建、模型训练到精度验证的完整分类流程;③支持水资源管理、生态环境监测等应用领域的空间数据分析; 阅读建议:此资源以实际代码形式呈现,建议结合GEE开发环境边运行边学习,重点关注影像预处理方法、训练样本构建逻辑、分类器参数设置及精度评估指标解读,便于迁移应用于其他区域或传感器数据。

65,198

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧