标签: while-loop

使用xcode学习C(通过K&R)

我正在用C编程语言(K&R)学习C 语言.

由于我不特别想在文本编辑器和运行gcc之间来回跳转,所以我决定使用xcode作为IDE.到目前为止,在第1.5.2节之前,我已经能够顺利地阅读本书的示例.

给定有效(?)程序时......

#include <stdio.h>

void main()
{
    long nc;

    nc = 0;
    while (getchar() != EOF)
        ++nc;
    printf("%ld\n", nc);
}
Run Code Online (Sandbox Code Playgroud)

...我没有收到最终输出,告诉我输入中有多少个字符.我通过xcode控制台窗口输入我的输入.

经过一些调试,看起来我的程序卡在while循环中,并且永远不会遇到EOF令牌.为了适应这一点,我已经不是取代换行符作为新的条件下,通过更换EOF用"\n",也什么都不做,并给了我一个int值指针比较预警.

我在这做错了什么?

我可以使用xcode关注K&R吗?

c xcode kr-c while-loop

3
推荐指数
1
解决办法
1011
查看次数

虽然循环帮助

嘿家伙......再次新手:)我正在组合一个计算三角形或正方形区域的程序,然后提示用户是否希望计算另一个.我已经得到了代码,它将计算任一形状的区域,但不会继续其余的代码.例如,选择方形,计算面积,然后返回到方形边的提示.我假设它永远是while循环,但不知道如何阻止循环无休止地继续.

继承我的代码:

 #include<stdio.h>
 #include<math.h>

 int main(void)

 {
     float sq_side, tri_base, tri_height, Area;
     char shape, cont = 'Y';

     printf("Please select the type of shape you desire to calculate the area for:\n");
     printf("                                                                     \n");
     printf("   Square = S                             Triangle = T               \n");
     printf("   -------                                    x                      \n");
     printf("   :     :                                   x x                     \n");
     printf("   :     :                                  x   x                    \n");
     printf("   -------                                 xxxxxxx                   \n");
     printf("                                                                     \n");
     printf("Please select either S or T:");
     scanf("%c", &shape);
     while (cont != 'n' && cont != 'N')

        if (shape …
Run Code Online (Sandbox Code Playgroud)

c while-loop

3
推荐指数
1
解决办法
1272
查看次数

虽然循环,似乎什么都没做?

嗨,我正在尝试在C++中创建一个函数,它接受一个数字,我,并通过循环运行找到它的倍数来决定它是否是素数,然后确保它不是通过一个素数系列测试.然而,似乎循环甚至没有被贯穿.无论循环在哪里,我都告诉它输出,但我没有输出.这是代码:

#include <iostream>

using namespace std;

int main()
{
    int j =1;
    int z = 0;
    int i = 10;
    bool p = false;
    while (p = false){
        cout << "not starting ifs";
        z=i%j;
        if (z==0 && j>2){
        p=true;
        cout << "not prime" << endl << "loops to if";
        }
        else if (j==1){
            j++;
            cout <<"loops to else if 1";
            }
        else if ( i==2 || j==i ){
          p = true;
          cout << "prime" << endl << "loops to else …
Run Code Online (Sandbox Code Playgroud)

c++ iteration loops operators while-loop

3
推荐指数
2
解决办法
388
查看次数

使用动态创建的复选框和数组维护复选框值

我试图让我的复选框值保存在我动态创建的输入上,而我的失败是悲惨的.是的我在线阅读了十几个或更多的教程,但我找不到一个迎合动态复选框列表的...请设置我的海峡!

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="checkboxForm">
<?php
$checkbox[] = array();
while($row = mysql_fetch_array($result)) {

$checked = isset($_POST['checkbox']) ? " checked" : "";

echo "<input name=\"checkbox[]\" type='checkbox' value='" . $row['first_name'] . "'     $checked /> ";
echo $row['first_name'];
echo "<hr />";
//print_r( $_POST['checkbox']);
}

if(isset($_POST['checkbox']) && !empty($_POST['checkbox'])) {
 foreach($_POST['checkbox'] as $checkbox){
echo $checkbox . "<br />";
} }
?>
Run Code Online (Sandbox Code Playgroud)

php arrays checkbox while-loop

3
推荐指数
1
解决办法
9212
查看次数

循环没有正确执行

class cga(object):
    ''''''
    def __int__(self,i,o):
        ''''''
        self.i = i
        self.o = o

    def get(self):
        ''''''
        self.i = []
        c = raw_input("How many courses you have enrolled in this semester?:")
        cout = 0
        while cout < c:
            n = raw_input("plz enter your course code:")
            w = raw_input("plz enter your course weight:")
            g = raw_input("plz enter your course grade:")
            cout += 1 
            self.i.append([n,w,g])
if __name__ == "__main__":
    test = cga()
    test.get()
Run Code Online (Sandbox Code Playgroud)

我的问题是如果我输入5,当程序询问我注册了多少课程.循环不会停止,程序将继续询问输入课程代码的重量等级.我调试时显示程序有cout = 6,但是它与c进行比较而while循环不会停止.

python debugging comparison raw-input while-loop

3
推荐指数
1
解决办法
107
查看次数

尝试使用Ruby while循环查找字符串的元音

def count_vowels(string)
  vowels = ["a", "e", "i", "o", "u"]
  i = 0
  j = 0
  count = 0

  while i < string.length do
    while j < vowels.length do
      if string[i] == vowels[j]
        count += 1
        break
      end

      j += 1
    end

    i += 1
  end

  puts count 
end
Run Code Online (Sandbox Code Playgroud)

我无法发现出错的地方.如果该程序遇到辅音,它就会停止.另外,使用".each"方法如何解决同样的问题?

ruby arrays while-loop

3
推荐指数
1
解决办法
3235
查看次数

打破一会儿循环,一口气切换

关于C ++中的这个问题,存在类似的问题,但是我在这里使用JavaScript。我基本上和其他帖子中的OP处于相同的情况。

var input = prompt();
while(true) {
    switch(input) {
        case 'hi':
        break;
        case 'bye':
            //I want to break out of the switch and the loop here
        break;
    }
    /*Other code*/
}
Run Code Online (Sandbox Code Playgroud)

反正有这样做吗?

我/*Other code*/在代码也应该中断的部分中也有多个开关。

javascript break while-loop switch-statement

3
推荐指数
1
解决办法
1770
查看次数

用于查找素数的Python while循环

作为Python的第一个练习,我正在尝试使用循环编写程序来查找素数.一切都与for循环一起工作所以我试图使用while循环.这有效,但程序返回一些不正确的数字.

import math
# looking for all primes below this number
max_num = int(input("max number?: "))

primes = [2]  # start with 2
test_num = 3  # which means testing starts with 3

while test_num < max_num:
    i = 0
    # It's only necessary to check with the primes smaller than the square
    # root of the test_num
    while primes[i] < math.sqrt(test_num):
        # using modulo to figure out if test_num is prime or not
        if (test_num % primes[i]) == 0: …
Run Code Online (Sandbox Code Playgroud)

python primes while-loop python-3.x

3
推荐指数
1
解决办法
7908
查看次数

在"A:"中对A进行了什么操作?

让我们说:

class Bar:
    pass
A = Bar()

while A:
    print("Foo!")
Run Code Online (Sandbox Code Playgroud)

然后调用什么操作A来确定while循环?

我尝试过,__eq__但没有做太多.

python class operation while-loop python-3.x

3
推荐指数
1
解决办法
45
查看次数

python的控制流语句的性能

我正在阅读关于循环的Python wiki ,它说

列表推导也在2.0版本中添加到Python中.它们提供了一种语法上更紧凑,更有效的编写上述for循环的方法:

但是,我发现当我测试这个时,我得到了一些意想不到的结果.

In [22]: def while_loop(n):
    ...:     i = 0
    ...:     while i < n:
    ...:         i+=1
    ...:

In [23]: def while_loop_2(n):
    ...:     while n > 0:
    ...:         n-=1
    ...:

In [24]: def for_loop(n):
    ...:     for _ in range(n):
    ...:         pass
    ...:

In [30]: %timeit(for_loop(1000000))
10 loops, best of 3: 23.9 ms per loop

In [31]: %timeit(while_loop(1000000))
10 loops, best of 3: 37.1 ms per loop

In [32]: %timeit(while_loop_2(1000000))
10 loops, best of 3: 38 …
Run Code Online (Sandbox Code Playgroud)

python loops for-loop while-loop

3
推荐指数
1
解决办法
106
查看次数