【问题标题】:How to make a program restart infinitely using while loops in C如何使用C中的while循环使程序无限重启
【发布时间】:2016-10-20 09:02:54
【问题描述】:

我是一个学习 C 语言的初学者,我有一个任务要求我编写一个程序来确定用户输入的数据作为三角形边是否有意义。

如果是,我要判断三角形的类型,如果不是,就应该写出“它不是三角形”这样的输出。

为此我必须使用while 循环,并且不允许使用do while

这就是我编写的代码。我正在使用 DevC++ 程序。

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    int a;
    int b;
    int c;
    printf("Enter the first side:\n");
    scanf("%d", &a);
    printf("Enter the second side:\n");
    scanf("%d", &b);
    printf("Enter the third side:\n");
    scanf("%d", &c);
    while ((a > 0) && (b > 0) && (c > 0))
    {
        if (((a + b) > c) && ((a + c) > b) && ((b + c) > a))
        {
            if ((a == b) && (b == c))
            {
                printf("That is an EQUILATERAL triangle\n");
                break;
            }
            if ((a == b) || (b == c) || (a == c))
            {
                printf("That is an ISOSCELES triangle\n");
                break;
            }
            else
            {
                printf("That is a SCALENE triangle\n");
                break;
            }
        }
        else
        {
            printf("That is not a TRIANGLE.\n");
            break;
        }
        return 0;
}

现在我遇到的问题是;每次我们决定它是否是三角形时,我被要求让程序重新启动(重复),但我不知道如何。我写的这个“中断”声明不起作用,我认为没有必要对吗?当我检查我的代码时也会出现这个错误

D:\Homework1.c  [Error] expected declaration or statement at end of input 

你能帮帮我吗?

Edit1:简单地说,循环应该总是重复,除非输入数字不是 0 或 (-)。因此,如果它们不是 0 或负数,用户将始终能够重新输入值(实际上这是循环的主要条件) Edit2:我纠正了丢失的 } 和 ((b + c) > c) 错误,谢谢大家。

【问题讨论】:

  • 你的任务是让它重复,你不能使用循环吗? ... 叹息,另一位有白痴任务的老师。使用 goto。
  • 这和 C++ 有什么关系?在某些情况下,使用两种语言进行标记是有意义的(其中一种是询问“比较和对比”类型的问题)。这不是其中之一。
  • @deviantfan:他们允许使用循环:只是while(...) { .... },而不是do { ... } while(...); 循环。
  • while (true) { /* 做任何你想做的事 */ }
  • 但在继续之前,您需要学习的第一件事是正确缩进您的代码。通过这样做,您应该会发现您缺少右括号。

标签: c loops while-loop dev-c++


【解决方案1】:

我在main 的末尾没有看到右大括号}(即在return 0 之后)。这可能是您的编译错误的原因。

【讨论】:

    【解决方案2】:

    在这里你可以有很多方法......

    1.使用标签,转到

    2.Go函数

    3.避免中断;

    4.修改代码

    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
        int flag=1;//for handling while loop
        while (flag)
        {
            //call a function to input 
            //call a function to process
            //call a function to output
            //flag = get input to repeat(1) or finish (0)
        }
        return 0;
    }
    

    不要使用 while 循环来检查 >0 条件。这是没有用的。 这里使用 if-else,因此不需要中断条件。 也处理

    【讨论】:

    • 如果很明显这是作业,我强烈建议不要给出完整的代码。只需给出大纲,让他/她学习。
    • 使用true 作为变量名是一个非常糟糕的主意,因为它在stdbool.h 中定义。
    • @PaulOgilvie 对不起,下次不重复了……不过他是初学者,所以他会从其他代码中学习。
    【解决方案3】:

    只需创建一个循环,while 在您的情况下运行无限条件。一开始没完没了,但让用户能够终止循环。

    1. 在循环开始时读取您的用户输入。
    2. 处理输入,因此判断它是否为三角形并将输出提供给用户
    3. 询问用户是要退出还是重新运行。

    #include <stdio.h>
    
    int main()
    {
        int exit = 0;  // surly you want to exit sometimes
        int a;
        int b;
        int c;
    
        while(!exit)
        {
            // Get your input
            printf("Enter the first side:\n");
            scanf("%d",&a);
            printf("Enter the second side:\n");
            scanf("%d",&b);
            printf("Enter the third side:\n");
            scanf("%d",&c);
    
            // check your input, process data if it is a triangle, give output
            // your code here
            printf("a:%d b:%d c:%d\n", a, b, c); // test code of mine
    
            // ask if the user wants to rerun
            printf("Exit? Yes(1) No(0):\n");
            scanf("%d",&exit);
        }
    
        return 0;
    }
    

    【讨论】:

    • 不确定这是否是 OP 想要的。
    • @MichaelWalz 你的意思是它应该运行直到找到一个三角形? exit 变量可以由 OP 的进程更新。但原理是一样的,在循环结束时,某人/某事决定是否需要重新运行。
    • 其实我并没有真正理解OP想要什么,这很不清楚。
    【解决方案4】:

    只需使用 while(1) 循环,输入并检查它,然后在每个结果后使用 continue

    【讨论】:

      猜你喜欢
      • 2013-02-22
      • 1970-01-01
      • 2015-01-19
      • 2016-08-27
      • 2015-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多