标签: while-loop

如何循环scanf_s()直到成功?

#include <stdio.h>

int main(){

    float x,y;

    printf("Enter 2 numbers: \n");
    scanf_s("%f %f", &x, &y);

    if (getchar() == '\n')
    {
    printf("Division: %f\n", x / y);
    printf("Product: %f\n", x * y);
    printf("Sum: %f\n", x + y);
    printf("Difference: %f\n", x - y);
    }
    else
    {
        printf("no Float-Value!");
    }

getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)

我们需要得到一个循环,所以如果我们输入错误的格式,程序应该再次要求输入两个数字

c format loops while-loop

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

如何在Perl中启动while循环之前打印?

我有这个代码Perl:

print "Processing ... ";
while ( some condition ) {
    # do something over than 10 minutes
}
print "OK\n";
Run Code Online (Sandbox Code Playgroud)

现在我在while循环结束后返回第一个打印件.

如何在启动while循环之前打印messeage ?

printing perl while-loop

1
推荐指数
2
解决办法
85
查看次数

while循环用分号java

为什么我们;在它之后使用带分号()的while循环?

我以此代码为例:

public static void Register()
{String name,code;
int posStudent,posCourse;
System.out.println("-----------------------------------------------");
System.out.println("Enter the name of the student");
name=in.next();

while ((posStudent=VerifyTheStudentName(name))==-1);

System.out.println("-----------------------------------------------");
System.out.println("The list of available courses is:");
for(int i=0;i<courses.size();i++)
    System.out.println(courses.get(i));
System.out.println("-----------------------------------------------");
System.out.println("Enter the course code");
code=in.next();
while((posCourse=VerifyTheCourseCode(code))==-1);
students.get(posStudent).registerTo(courses.get(posCourse));
}
Run Code Online (Sandbox Code Playgroud)

那么这里做什么呢?

java while-loop

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

以用户给出的一位数打印所有的narcisistic数字

我试图打印用户输入的所有数字的自恋数字.

例如,对于输入3,程序应该打印:153, 370, 371, 407.现在由于某种原因而不是打印数字,它没有打印任何东西,程序卡住了.

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

int main() {
    int digit, a, c = 0;
    unsigned long long int count, b, sum;
    printf("Enter digits to check narcisistic: ");
    scanf("%d", &digit);
    count = pow(10, digit - 1);
    if (digit > 2) {
        while (count < pow(10, digit)) {
            b = count;
            sum = 0;
            while (count >= 1) {
                a = b % 10;
                b /= 10;
                sum += pow(a, digit);
            }
            if (sum …
Run Code Online (Sandbox Code Playgroud)

c while-loop nested-loops

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

如何通过while循环以pythonically方式避免此代码重复?

我想找到第一个myfile????.txt尚不存在的文件名(???? 是一个数字).这有效:

import os
i = 0
f = 'myfile%04i.txt' % i
while os.path.exists(f):
    i += 1
    f = 'myfile%04i.txt' % i
Run Code Online (Sandbox Code Playgroud)

但我不喜欢代码重复f = ....

是否有一种pythonic方法来避免此while循环中的代码重复?

注意:我已经发布了一个半满意的解决方案,使用do/while成语,如在Python中模拟一个do-while循环的主要答案中所提到的那样?,但我仍然想知道这种特殊情况是否有更好的方法(因此,这不是这个问题的愚蠢).

python loops code-duplication while-loop

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

如何在C中使用此代码进入while循环

我刚开始学习C语言,并尝试创建自己的函数.但是,我不明白为什么我的while循环不在此代码中运行.我尝试了很多方法,比如==,!=,<,> ......等,但我不明白为什么它运行不正常.

PS.我误解了什么是因子:(我改变了你所有人的建议代码.谢谢大家

#include <stdio.h>

int my_iterative_factorial(int nb)
{
  int i;
  int store;

  i = 1;
  if(nb == '0')
    return(0);
  while (nb > 0)
  {
    i = i * nb;
    nb--;
  }
  return(i);
}

int main(void)
{
  int nb;

  nb = 5;
  printf("%d\n",my_iterative_factorial(nb));
  return(0);
}
Run Code Online (Sandbox Code Playgroud)

c while-loop

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

为什么我的阶乘函数返回NaN?

我使用递归和while循环编写了一个阶乘函数,但无论何时调用它,它的返回值都是NaN.我想知道为什么吗?以及如何解决它?

功能

function factorial(n) {
    while(n > 0)
        return factorial(n - 1) * n;
}
Run Code Online (Sandbox Code Playgroud)

javascript recursion nan factorial while-loop

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

在Python中使用for循环读取文件的内容

这是问题所在.我有names.txt文件.该文件的内容如下.该问题要求显示此文件中的名称数量.我可以用while循环.它有效,没问题.但出于某种原因,如果我想用for循环这样做,它会给我错误的名字数量.

    Julia Roberts 
    Michael Scott
    Kevin Malone
    Jim Halpert
    Pam Halpert
    Dwight Schrute
Run Code Online (Sandbox Code Playgroud)

这是while循环.它运行完美.

def main():
    try:
        # open the names.txt file in read mode.
        infile=open("names.txt", "r")

        # set an accumulator for number of names
        numbers_of_name=0.0

        # read the first line
        line=infile.readline()

        # read the rest of the file
        while line!="":
            numbers_of_name+=1
            line=infile.readline()

        # print the numbers of names in the names.txt file.    
        print("There are", int(numbers_of_name), "names in the names.txt file.")

        # close the file
        infile.close() …
Run Code Online (Sandbox Code Playgroud)

python for-loop while-loop

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

在xpath = no这样的元素时停止循环

我试图在没有显示xpath时停止while循环.但是,代码会停止并显示NoSuchElementException消息.这是我的代码,

 n=1
    while n<100:


        n+=1
        time.sleep(10)

#       

        Data=driver.find_element_by_xpath('.//tbody//tr//td//span//a[text() != ""]').is_displayed()

        if Data == True:

           ...

       if Data == False: 
           break 
Run Code Online (Sandbox Code Playgroud)

但我得到了:

"NoSuchElementException:消息:没有这样的元素:无法定位元素:{"method":"xpath","selector":".// tbody // tr // td // span // a [text()!= ""] "}"

这正是我想要停止循环...当元素不可定位时.

python selenium for-loop while-loop

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

如何在for循环中突破while循环

我需要彻底打破while循环(空检查),然后转到外部for循环的下一个迭代。

我试过了

for(Product: product:ListofProducts){
 while(null!=product.getDate){
    if(product.getDate>specifiedDate){
        doOnething()
    }
    else{
        doAnotherThing()
    }
    continue;
}
Run Code Online (Sandbox Code Playgroud)

如果产品日期不为null,并且执行onething()或anotherthing(),那么我想转到f​​or循环的下一个迭代

java for-loop while-loop

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