【问题标题】:my program overlooks 'while' loop我的程序忽略了“while”循环
【发布时间】:2015-11-23 02:05:57
【问题描述】:

我需要编写一个程序,计算 3^2+6^2+9^2+ ... +(3×N)^2,对于给定整数 N 的用户。程序提示用户输入小于 20 的整数。程序应在 3 次错误机会后停止。这是我的代码:

#include <stdio.h>
int main(){
        int n,x,i ;
        float p;
        printf("Hey give me a positive integer number smaller than 20 \n ");
        scanf("%d" ,&n);
        x=3;
        p=0;
        while ((n<=0) && (n>=20)){
                printf("wrong input %d chances left \n" ,x);
                x--;
                if (x==0) return 0;
                scanf("%d" , &n);
        }
        for ( i=0; i<=n; i++){
                p= (3*i)* (3*i) + p ;
        }  
        printf("Yeah.. thats the result bro %f \n" , p);
        return 0;
}

我不知道为什么它不会进入 while 循环。请帮忙。

【问题讨论】:

    标签: c


    【解决方案1】:
    while ((n<=0) && (n>=20)){
    

    查看此循环中的条件,您是否知道小于或等于0 且大于20 的任何数字? .

    此循环仅适用于此类数字,因为您使用了 &amp;&amp; 运算符,仅当两个条件都满足时才会为真(因此您的循环不会迭代)。

    为您的循环使用|| 运算符-

    while ((n<=0) || (n>=20)){
    

    【讨论】:

      猜你喜欢
      • 2020-01-30
      • 2016-09-08
      • 2013-08-04
      • 2021-05-31
      • 2017-04-29
      • 2021-10-17
      • 2019-11-07
      • 1970-01-01
      • 2017-06-18
      相关资源
      最近更新 更多