- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个代码,对于一行或几行字符串,查找整个输入是否只有“cool”(它的第一个中间字符串和最后一个字符串相同)行,只有“uncool”行或混合两者。
我遇到的问题是,每当我输入偶数时,while 循环就会终止。调试我发现,就在跳出之前 n 得到值 0 但我不明白这将如何使循环结束。
这是代码:
#include <iostream>
using namespace std;
int main () {
// Bool has control if we have found a cool line/non-cool line
bool cool = false;
bool uncool = false;
int n; //lenght of input
while (cin >> n) {
if (cool and uncool) break; // we have found one of each so we know it is a mixed input
else if (n%2 == 0) uncool = true; // if the lenght is even there is no middle string
else {
// we are trying to see if the middle and last string are equal to the first
string comparing_string;
cin >> comparing_string;
string rest_of_sequence;
bool this_is_cool = true;
for (int i = n-2; i >= 0; i--) { // we input the rest of strings and compare them to the first
cin >> rest_of_sequence;
if ((i == n/2 or i == 0) and rest_of_sequence != comparing_string) this_is_cool = false;
}
if (this_is_cool) cool = true;
else uncool = true;
}
}
if (cool and uncool) cout << "both types" << endl;
else if (cool and not uncool) cout << "all cool" << endl;
else if (uncool and not cool) cout << "none cool" << endl;
}
感谢任何帮助!我目前在大学一年级,并且总是愿意接受推荐的书籍/网页/视频以继续学习:)
最佳答案
问题是我认为程序会忽略 while 循环中不是整数的输入,但事实并非如此。
现在代码是正确的:
else if (n%2 == 0) {// if the lenght is even there is no middle string
uncool = true;
string just_passing_input;
for (int i = n; i > 0; i--) cin >> just_passing_input;
}
感谢您的有益反馈,我现在将继续学习。
关于c++ - 在特定情况下跳出循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34558652/
这个问题刚刚出现:如何打破 if 语句?我有一个很长的 if 语句,但有一种情况我可以尽早摆脱它。 在循环中我可以这样做: while (something ) { last if $some
我有一个 iframe,它显示带有可点击链接的 flash 横幅广告。无论如何强制链接在父窗口中打开,而不将 JS 放在打开或更改 flash 文件的页面上? 例如,我可以对 iFrame 做些什么来
JSHint 告诉我不要在 if 语句中使用标签(就 jshint 而言,标签似乎只用于循环)。 forminjection: if (options.addToForm !== false) {
我正在动态地将文本字段(最多 32 个)添加到我的页面中,我需要遍历所有这些文本字段并检查,如果它们全部为空,我将显示一条警告消息!我不想使用 document.getElementsByTagNam
我正在编写一个程序,其中我使用 if 语句来检查某些条件;如果为真,我会增加一个计数器。问题是,一旦语句为真,变量要么无限递增,要么随机递增。 如果条件满足但我一直在尝试使用一些子句来打破这个语句 我
有一个similar question关于 C++ 中的这个问题,但我在这里使用 JavaScript。我基本上和另一篇文章中的 OP 处于相同的情况。 var input = prompt(); w
我一直在研究 Issue 14在 PascalScript 脚本引擎上,使用 Goto 命令跳出 Case block 会产生编译器错误,即使这是完全有效(如果丑陋)的 Object Pascal 代
我正在使用 VBA 的 While...Wend 循环。 Dim count as Integer While True count=count+1 If count = 10 The
我在这个网站上看到过类似的问题,但我无法让它工作。我正在尝试通过 Javascript 检测输入框中的重复条目 - 但我希望在输入重复项时循环中断。我有那部分工作,但循环继续运行,它创建了一个用户无法
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我有一个问题,我可能需要因为某些值(value)而跳出整个 promise 链,或者根据一个值(value)采取两条路径。 你怎么做最好? 这是我想跳出整个链条的第一个场景。我只想给他们留言。
因此,对于类作业,我必须接受用户输入并分配我的沙鼠对象每天可以使用的“食物”量。在这种情况下,我已经从用户那里获取了每天的最大食物量,如果用户尝试输入超过每日最大值的值,我需要向用户提供错误消息。 如
我正在尝试制作一个导航栏,它看起来像这样 https://imgur.com/a/wf7Pe 是有链接的那个,忽略白色的div。 问题是,如果调整页面大小,文本会超出 div,位于其下方。 像这样:
我正在尝试使用 C 退出 while 循环,但使用中断、返回或更改 while 条件不起作用。 基本上,我想知道为什么在最后一个 else if 中,当我声明 back = true 时,我的代码一直
我正在寻找关键字或干净的方式来不使用选择语句退出finally block 。考虑以下示例: private bool AtteptToDoSomething() { try {
我有一个 python 脚本,它将解析 xml 文件中的序列号并将它们写入文本文件。下面代码的问题是,它正在进行无限循环。如果我在记录到文件后的某个位置添加一条break语句,它只会写入一个序列号。如
我在类 LinkRepository 中有一个方法,我正在检查 vector 数组 Datalinks 中的重复条目,它是该类的一个成员。我遍历数组中的所有元素以检查新条目 Datalink* dat
是否可以在每个循环中跳出下划线……? _.each(obj, function(v,i){ if(i > 2){ break // <~ does not work } // so
我想优化依赖于 filter() 的函数。在某些情况下,我想在它们达到特定元素时打破它们。 (例如,我可能有一个包含不同元素的数组。或者,我只想实现一种 findFirst 功能。)在这种情况下,函数
我是一名优秀的程序员,十分优秀!