简体   繁体   English

不询问新输入,在 C 的 while 循环内

[英]New input isn't asked, Inside a while loop in C

我的代码 I have been trying to solve the problem set 1 in CS50, language C. I've come to this point, but I got stuck in here.我一直在尝试解决CS50中的问题集1,语言C。我已经走到这一步了,但是我卡在这里了。 I want my code to ask for a new input while(n>=9 || n<=0) but it ends there, instead of asking for a new input.我希望我的代码要求一个新的输入 while(n>=9 || n<=0) 但它到此为止,而不是要求一个新的输入。 I have already tried return n;我已经试过 return n; but it didn't work at all.但它根本不起作用。 You can see the console and the results.您可以看到控制台和结果。

When I asked my code to return 0;当我要求我的代码返回 0 时; I thought it would be asking for a new input.我认为它会要求一个新的输入。 But as it can be seen, it ended up.但正如它所看到的那样,它结束了。 What I want is it to ask for a new input, instead of stop working.我想要的是要求新的输入,而不是停止工作。

This is my first time and post in here, so I hope I have described my problem good enough.这是我第一次在这里发帖,所以我希望我已经足够好地描述了我的问题。

#include <stdio.h>
#include <cs50.h>
int main(void)
{
int n = get_int("Number: ");
while(n>=9 || n<=0)
{
  return 0;
}
int i;
for(i=0;i<n;i++)
{
 int a;
  for(a=n-1;a>i;a--)
  {
    printf(" ");
  }
  int y;
  for(y=0;y<=i;y++)
  {
    printf("#");
  }
    printf("\n");
}

}

As others suggested, return 0 is an action that terminates the current function you are in, and returns said value.正如其他人所建议的那样, return 0是终止您当前所在的 function 并返回所述值的操作。 I don't know what is your level of knowledge of C, I'll try to put less informations possible here.我不知道你对 C 的了解程度如何,我会尽量在这里提供更少的信息。 Just know that if you write return inside your main(){} block, the program will ignore all the following code and end the program if that return is executed.只要知道,如果您在main(){}块中编写 return,程序将忽略以下所有代码,并在执行 return 时结束程序。

Now as to how to get your desired result现在关于如何获得您想要的结果

I want my code to ask for a new input while(n>=9 || n<=0)我希望我的代码要求新输入 while(n>=9 || n<=0)

it seems you just need to put your statement inside the while loop:看来您只需要将语句放在 while 循环中:

int n=0;
while (n>=9 || n<=0)
{
    n = get_int("Number: ");
}

in this case you need to declare int n outside the loop since you will use it later.在这种情况下,您需要在循环外声明int n ,因为您稍后会用到它。 (I initialized it as 0 to let the program enter the loop). (我初始化为0让程序进入循环)。

If you have studied and are able to use "do while" loops, it may be your best choice here.如果您学习过并且能够使用“do while”循环,那么这里可能是您的最佳选择。 If your program needs to ask for input, and keep asking until input is no longer in the range (n >= 9 || n <= 0) you might want to try如果您的程序需要询问输入,并一直询问直到输入不再在范围内(n >= 9 || n <= 0)您可能想尝试

int n=0;
do
{
    n = get_int("Number: ");
} while (n>=9 || n<=0)

This will execute the block of code once for sure, then checks if the while condition is met and loops again.这将确定执行一次代码块,然后检查是否满足 while 条件并再次循环。 The outcome is you get the statement executed once and then until the condition is no longer met.结果是你让语句执行一次,然后直到不再满足条件。

Put your code inside while loop to ask it whenever (n >= 9 || n <= 0) because right now whenever n is greater or equal 9 or less or equal 0 your your program will end because return 0 ends actual function and in your case it is main() function将您的代码放在 while 循环中以在(n >= 9 || n <= 0)时询问它,因为现在只要 n 大于或等于 9 或小于或等于 0,您的程序就会结束,因为return 0结束实际 function 并且在你的情况是main() function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM