Another_Mark = raw_input("would you like to enter another mark? (y/n)")
while Another_Mark.lower() != "n" or Another_Mark.lower() != "y":
Another_Mark = raw_input("Do you want to input another mark? Please put either 'y' or 'n' this time")
if Another_Mark == "y":
print "blah"
if Another_Mark == "n":
print "Blue"
Run Code Online (Sandbox Code Playgroud)
这不是我正在使用的实际代码,除了前三行.无论如何我的问题是为什么while循环仍然重复,即使我输入值'y'或'n',当它再次询问你是否想在第三行输入另一个标记时.我陷入了一个无限重复的循环中.当Another_Mark的值更改为"y"或"n"时,不应重复此操作
我一直在运行这个并输入"12+"作为表达式.当它到达while循环时,它会被卡住,好像从未遇到过这种情况.我不明白为什么这是因为在while循环之前"它"等于2所以甚至不应该使用循环.
//array based stack implementation
class Stack
{
private:
int capacity; //max size of stack
int top; //index for top element
char *listArray; //array holding stack elements
public:
Stack (int size = 50){ //constructor
capacity = size;
top = 0;
listArray = new char[size];
}
~Stack() { delete [] listArray; } //destructor
void push(char it) { //Put "it" on stack
listArray[top++] = it;
}
char pop() { //pop top element
return listArray [--top];
}
char& topValue() const { //return top …Run Code Online (Sandbox Code Playgroud) 我已经阅读了很多关于getchar()它及其行为的问题,但我仍然不明白这个简单的代码..
while (scanf("%d", &z) != 1)
{
while (getchar() != '\n');
printf ("Try again: ");}
Run Code Online (Sandbox Code Playgroud)
这段代码是为了验证字符..我从这段代码推断出来的是,如果我输入的话
Stackoverflow
Run Code Online (Sandbox Code Playgroud)
然后用换行符'\n'将整行推送到缓冲区.然后getchar()从缓冲区读取每个字符并返回一个整数,清理缓冲区.在这种情况下,while循环应循环12次(数量为Stackoverflow中的字符直到它到达'\n'字符..但实际上它只循环一次,输出是
Try again:
Run Code Online (Sandbox Code Playgroud)
意味着它的循环scanf再次要求...它违背了我对循环的理解..也许我误解了循环..另外一个问题,如果getchar()返回整数然后如何将它与'\n'之类的字符进行比较?
我正在尝试创建一个while循环,提示用户在使用无效条目时重新输入数据.在我的程序中,我要求用户输入一个数字(0-3),所以如果他们输入的不是其他内容我想要弹出一条错误信息,要求他们重新输入他们的数据.我不知道我在循环中做错了什么.无论我输入什么,我总是得到错误信息.
String itemChoice = JOptionPane.showInputDialog(null, itemList);
itemChoiceNum = Integer.parseInt(itemChoice);
//test to make sure the input is valid
while (itemChoiceNum != 0 || itemChoiceNum != 1 || itemChoiceNum != 2 || itemChoiceNum != 3) {
JOptionPane.showMessageDialog(null, "Sorry that's not a valid choice. Please start again.");
itemChoice = JOptionPane.showInputDialog(null, itemList);
}
Run Code Online (Sandbox Code Playgroud)
NumberFormatException的
while (itemChoiceNum != 0 && itemChoiceNum != 1 && itemChoiceNum != 2 && itemChoiceNum != 3) {
try {
JOptionPane.showMessageDialog(null, "Sorry that's not a valid choice. Please start again.");
itemChoice …Run Code Online (Sandbox Code Playgroud) 我正在使用嵌套的while循环,并遇到问题,因为内循环只运行一次.为了证明我已经做了一些测试代码.
#include <stdio.h>
int main(){
int i = 0;
int j = 0;
while(i < 10){
printf("i:%d\n", i);
while(j < 10){
printf("j:%d\n", j);
j++;
}
i++;
}
}
Run Code Online (Sandbox Code Playgroud)
返回:
i:0
j:0
j:1
j:2
j:3
j:4
j:5
j:6
j:7
j:8
j:9
i:1
i:2
i:3
i:4
i:5
i:6
i:7
i:8
i:9
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释为什么嵌套循环不会执行10次?我该怎么做才能解决这个问题?
我一直在寻找大约一个小时,根本无法找到任何解决方案.我希望我的.txt文件中的每一行都添加到我的列表中,只要它以相同的数字开头即可.问题在于"line.Substring(0,1)",我不知道如何解决它.也许你明白我想要实现的目标.
using (StreamReader file = new StreamReader(schedulePathDays))
{
string line;
while ((line.Substring(0, 1) = file.ReadLine()) == thisDay.ToString())
{
exercisesRow.Add(line);
}
}
Run Code Online (Sandbox Code Playgroud) 我希望stop=true在内部for循环中达到条件时打破外部while 循环.这可能吗?
urlsToScrape.addAll(startUrls);
boolean stop=false;
while(stop==false){
for(Link url:urlsToScrape){
p.rint(url.depth+" >= "+depth);
if(url.depth>=depth){
p.rint("STOP!");
stop=true;
break;
}
p.rint("scrape(): "+url.link+" / "+url.depth);
processLinks(url.link,url.depth);
urlsToScrape.remove(url);
scrapedUrls.add(url);
}
}
Run Code Online (Sandbox Code Playgroud) 我有两个传感器,我需要不断监测.两个传感器的内部工作是不同的,我需要不断检查,如下所示.
int sensorA(){
while (true) {
// read sensor A
cout<<"sensorA \n";
}
}
int sensorB(){
while (true) {
// read sensor B
cout<<"sensorB \n";
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何确保以交错方式读取两个传感器?像读传感器A然后传感器B然后传感器A等.
我正在创建不同长度和方向的向量,并且有一些我无法解释的行为.
我从随机种子开始并声明一个向量:
srand(time(NULL));
std::vector<Particle> psystem;
Run Code Online (Sandbox Code Playgroud)
以下代码然后创建明显的"随机"向量:
float v[3] = {0};
for(int i = 0; i < vectors; ++i) {
v[0] = float(rand() % 200 - 100) / 3000;
v[1] = float(rand() % 200 - 100) / 3000;
v[2] = float(rand() % 200 - 100) / 3000;
Particle p(0, 0, 0, v[0], v[1], v[2]);
psystem.push_back(p);
}
Run Code Online (Sandbox Code Playgroud)
添加的while循环会导致psystem中的所有元素具有相同的精确值(例如,所有元素都具有[0.00345,-0.234,0.00701]):
const float min_length = 0.0174;
float v[3] = {0};
for(int i = 0; i < vectors; ++i) {
while(sqrt(v[0]*v[0] + v[1]*v[1] + …Run Code Online (Sandbox Code Playgroud) 我是新来的,至少问问题.一直都能在这里找到好的答案.试图回到编程和重新学习C但遇到了奇怪的问题.
#include <stdio.h>
main()
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld \n", nc);
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,在输入任意数量的字符并按Enter后,它不会打印nc的值.点击进入后,我可以再次开始打字,同样的故事.真的看不出有什么不对.唯一可行的方法是将++ nc和printf放在括号内.但是当我按下回车键时,它会给出1到nc的值,这不是我想要的.我只想要nc.毋庸置疑,这种类型也不是问题.提前致谢