2015-11-22 55 views
1

我需要编写一个程序,计算3^2 + 6^2 + 9^2 + ... +(3×N)^ 2,给定一个给定整数N的用户。程序会提示用户输入一个整数,是小于20.该方案应经过3个错误chances.This站是我的代码:我的程序忽略'while'循环

#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循环。请帮忙。

回答

5
while ((n<=0) && (n>=20)){ 

看到在这个循环条件,你知道的任何数量的为小于或等于0,比20更大? 。

此循环只适用于这样的数字,因为您使用了&&运算符,只有当两个条件都满足时才会成立(因此您的循环不会迭代)。

使用||运营商的环 -

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