标签: while-loop

在foreach内继续

在下面的C#代码片段中,
我在while循环中有一个' foreach'循环,我希望在foreach某个条件发生时跳转到' '中的下一个项目.

foreach (string objectName in this.ObjectNames)
{
    // Line to jump to when this.MoveToNextObject is true.
    this.ExecuteSomeCode();
    while (this.boolValue)
    {
        // 'continue' would jump to here.
        this.ExecuteSomeMoreCode();
        if (this.MoveToNextObject())
        {
            // What should go here to jump to next object.
        }
        this.ExecuteEvenMoreCode();
        this.boolValue = this.ResumeWhileLoop();
    }
    this.ExecuteSomeOtherCode();
}
Run Code Online (Sandbox Code Playgroud)

' continue'会跳到' while'循环的开头而不是' foreach'循环.这里有一个关键字,或者我应该使用我不喜欢的goto.

c# foreach continue keyword while-loop

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

Makefile while 循环

即使将 SHELL 设置为 bash 而不是 sh,以下操作也不起作用:

doc:
    while read line; do \
      eval echo "$$line" > $(DOC) \
    done < $(DOC).templ

/bin/bash: -c: line 3: syntax error: unexpected end of file
Run Code Online (Sandbox Code Playgroud)

我想要做的是有一个模板文件,其中包含 bash 参数理解等 ( $(), ${}) 并在编译时“构建”它。有一个更好的方法吗?

bash templates makefile while-loop

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

Java For 循环到递归函数中

public class For {
 public static void main(String[] args){
          for(int i=2; i<=1024; i *= 2){
           System.out.println("Count is: " + i);
      }
 }




public class While {
    public static void main(String[] args){
        int i = 1;
        while (i < 1024) {
            i *= 2;
            System.out.println("Count is: " + i);
      }
 }


public class DoWhile {
     public static void main(String[] args){
        int i = 1;
        if (i < 1024) {
            do { i*=2;
                System.out.println("Count is: " + i);
            } while (i < …
Run Code Online (Sandbox Code Playgroud)

java recursion for-loop while-loop

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

while 循环中的 try-catch 方法?

我有这个代码,我想把 try-catch 放在一个 while 循环中。逻辑是,“当有输入错误时,程序会继续要求正确的输入”。我将如何做到这一点?提前致谢。

public class Random1 {

  public static void main(String[] args) {

    int g;

    Scanner input = new Scanner(System.in);
    Random r = new Random();
    int a = r.nextInt(10) + 1;


    try {
        System.out.print("Enter your guess: ");
        g = input.nextInt();
        if (g == a) {

            System.out.println("**************");
            System.out.println("*  YOU WON!  *");
            System.out.println("**************");
            System.out.println("Thank you for playing!");

        } else if (g != a) {
            System.out.println("Sorry, better luck next time!");
        }
    } catch (InputMismatchException e) {
        System.err.println("Not a valid input. Error …
Run Code Online (Sandbox Code Playgroud)

java try-catch while-loop

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

减少随机数...逻辑错误

基本上我试图用这个程序做的是让系统生成一个随机数,然后将该数字分配给另一个变量.如果数字大于20,那么while循环将开始,直到变量的值(由随机数确定)小于20,然后结束程序.问题是,while循环似乎永远持续下去:(请帮助

这是代码:

package innocence;
import java.util.Scanner;

class GS1{

    public static void main(String[]args){


        int ranNum = (int)( Math.random()*25);
        int num=ranNum;
        System.out.println(num);
        while(num > 20){

        System.out.println("Not acceptable");   

        num= num --;
        }       
    }    
}
Run Code Online (Sandbox Code Playgroud)

java while-loop

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

简单的C++阶乘程序

作为家庭作业,我需要一个读取非负整数的程序,并计算并打印其阶乘.到目前为止我编写了代码,但如果我尝试输入50!结果为0.它适用于较小的数字.任何帮助将非常感激.

#include <iostream>

using namespace std;

int main()
{
    int counter = 1;
    int number;

    cout << "Please enter a number: ";
    cin >> number;

    int factorial = number;
    while (counter != number)
    {
        factorial = factorial * (number - counter);
        counter++;

    }

    cout << "The factorial of " << number << "! is: " << factorial << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ integer factorial while-loop

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

为什么这些continue语句不能在我的循环中工作?

我正在尝试将以下for循环转换为while和do-while循环,但由于某种原因,代码在进入实际循环之前中断.

这是我想要复制的内容:

#include <stdio.h>

int main(void)
{ int y;

  for (y=1;y<10; y++)
  { if (y == 5)
      continue;
    printf("%2d", y);
  }

  printf("\ny=%2d\n",y);

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

以下是我尝试制作等效的while和do-while循环:

#include <stdio.h>

int main(void)
{
  int y;

  y = 1;
  while(y < 10)  // here's my while loop
    {
      if(y == 5)
        continue;
      printf("%2d", y);
      y++;
    }

  printf("\ny=%2d\n",y);

/********************
... and my do-while
********************/
  y = 1;
  do
    {
      if (y == 5)
        continue;
      printf("%2d", y);
      y++;
    } while(y < 10);

  printf("\ny=%2d\n",y);

  return 0; …
Run Code Online (Sandbox Code Playgroud)

c continue while-loop do-while

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

为什么我的while循环即使在值为true时也会继续执行?

我最初尝试使用Do While循环但是与while循环具有相同的结果.用户必须输入数字28-31.在第一次尝试时,如果用户正确输入值,它将转到代码的下一部分.但是,如果用户输入的值不正确,它将再次询问一个号码,但无论他们输入什么,它都会不断重复.

#include <cs50.h>
#include <stdio.h>

 int main(void)
 {

     printf("Days in month: ");
     int daysInMonth = GetInt();    

     while (daysInMonth < 28 || daysInMonth > 31)
     {

         printf("Days in month: ");
         int daysInMonth = GetInt(); 

         printf("%i\n", daysInMonth);

     }

     printf("Pennies on the first day: ");
     int pennies = GetInt();

     while (pennies < 1)
     {

        printf("Pennies on the first day: ");
        int pennies = GetInt();

     }

}
Run Code Online (Sandbox Code Playgroud)

printf语句用于调试目的,以测试是否daysInMonth重新分配值.

c scope loops while-loop cs50

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

python行而不是列

我正在学习使用python而且我坚持使用这段代码.

我想显示行,但我得到列.我尝试了不同的变化,我为while循环得到一行,但我仍然得到for循环的列.

Start_num = 0
Max_num = 200

while Start_num < 201:
    for Number in range (Max_num, -1, -25):
       print(Start_num, end=' ')
       Start_num += 25
       print(Number)
Run Code Online (Sandbox Code Playgroud)

这是输出当前查找此代码的方式.

0 200
25 175
50 150
75 125
100 100
125 75
150 50
175 25
200 0
Run Code Online (Sandbox Code Playgroud)

必须有一种方法来获得两行而不是列,请帮忙.

python loops for-loop rows while-loop

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

为什么"结束"仍然添加到列表中?

如果用户键入'end',则应该打印列表,并且'end'不应该添加到列表中while name != "end":,因为它是.

name = "";

names = []
while name != "end":
    name = input("Please enter a name")
    names.append(name)
    print("hi")

print(names)
Run Code Online (Sandbox Code Playgroud)

如果我输入鲍勃,吉姆,结束,列表应该是['Bob', 'Jim'],但它是['Bob', 'Jim', 'end']

python while-loop python-3.x

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