标签: do-while

循环开始停止时C#做

我试图为这个循环制作一个停止按钮,但它运行无限期,当我点击按钮2时没有任何反应

bool dowhile = false;
private void button1_Click(object sender, EventArgs e)
{
    do
    {
        for (int i = listbox1.Items.Count - 1; i >= 0; i--)
        {
            string textstring = listbox1.Items[i].ToString();
            richTextBox1.AppendText("" + textstring + ": Done\n");
            Thread.Sleep(1000);
        }
    } while (!dowhile);
}
private void button2_Click(object sender, EventArgs e)
{
    this.dowhile = true;
}
Run Code Online (Sandbox Code Playgroud)

我哪里出错了?

sry for"lvlchanger"错字,现在代码还可以,没有什么遗漏

我也在寻找一个不那么长的修复:))

c# loops for-loop while-loop do-while

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

do-while循环的替代语法(使用冒号)

本手册引用了大多数使用冒号的控制结构的替代语法,但我没有看到一个do-while循环.有可能这样做吗?像这样的东西:

//Simple example
$arr = array(1,2,3,4,5,6);
$i = 0;

do :
    echo $arr[$i].'<br/>';
    $i++;
while($i < count($arr)) endwhile;
Run Code Online (Sandbox Code Playgroud)

php syntax do-while

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

为什么这样做/永远不会结束?

它只是保持循环.数字继续减少,直到该计划结束.我滥用了什么吗?

playerHealth和orcHealth ints是100.

randomNumber = ("%10d", 1 + (rand() % 100));
Run Code Online (Sandbox Code Playgroud)

这是我在srand()解释页面上看到的随机数的方式.如果这是错的,应该怎么做?

这里还有其他问题吗?

    switch(charDecision)
{
case 1:
    cout << "FIGHT" << endl;
    do{
        randomNumber = ("%10d", 1 + (rand() % 100));
        if(randomNumber >= 50){
            orcHealth = orcHealth - (randomNumber - (randomNumber / 5));
        cout << "You hit the orc! He now has " << orcHealth << " life left!" << endl;
        }
        else
        {
            playerHealth = playerHealth - (randomNumber - (randomNumber / 5));
            cout << "The orc hit you! You …
Run Code Online (Sandbox Code Playgroud)

c++ infinite-loop do-while

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

局部变量不会初始化(Java)

我正在尝试编写一个简单的骰子滚动游戏,如果骰子匹配,那就是胜利,如果它们是一个数字,它是一个平局(junker),如果它们既不满足这些条件,那也是一种损失.

我正在使用do while循环,并且似乎无法获取局部变量来初始化:

import java.util.Scanner;

public class Program06 
{
public static void main(String[] args)
{
    Scanner stdIn = new Scanner(System.in);
    String response = "k";

    int d1 = 1;
    int d2;
    int win;
    int lose;
    int junker;

    System.out.println("Welcome to Computer Dice");
    System.out.println("---------------------------");
    System.out.println("\nYou will first roll your dice");

    System.out.println("Next the outcome of your roll will be determined:");

    System.out.println("Any pair and you Win");
    System.out.println("Anything else and you Lose");
    System.out.println("\n----------------------------");

    System.out.println();

    do
    {

    System.out.println("Player");
    System.out.println("----------");

    d1 = (int)(Math.random() * 6) + 1;
    d2 …
Run Code Online (Sandbox Code Playgroud)

java local-variables do-while

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

打印从 0 到 10,000 的质数

我目前正在尝试创建一个程序,该程序仅使用 for、do while 和 ifs 来打印从 0 到 10,000 的素数。我创建了这个程序,但它没有运行

static void Main(string[] args)
    { 
        for (int x = 2; x < 10000; x++)
        { 
            for (int y = 1; y < x; y++)
            { 
                if (x % y != 0)
                {
                    Console.WriteLine(x); 
                }  
            }
            Console.ReadKey();
        }
Run Code Online (Sandbox Code Playgroud)

我不知道问题出在哪里,也不知道 for inside 是否重置。

c# primes for-loop numbers do-while

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

为什么这些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
查看次数

C中的工作分配算法(做问题)

所以我正在开发一种算法来在线程之间均匀分配工作,但它有点独特.正在进行的工作是文件比较,其中我们有一个文件列表(比方说10个文件).我们不希望将文件1与文件1进行比较,因此每个文件都会跳过比较自身.文件1将自己与文件2到10进行比较,文件2将自己与文件3到10等进行比较.因此,当我们向下移动文件比较列表时,文件数比较下降.这是我遇到问题的小片段(特别是do-while循环)

// Get first pass file chunks
printf("Split approximation: ");
for (int i = 0; i < num_threads; i++)
{
    file_chunks[i] = (int)floor((double)num_file_compares / (double)num_threads);
    printf("%d ", file_chunks[i]);
}
printf("\n");

// Adjust file chunks to contain all files
for (int i = 0; i < num_threads; i++)
{
    file_chunk_sum = file_chunk_sum + file_chunks[i];
}
append_count = num_file_compares - file_chunk_sum;
printf("Need to append last %d\n", append_count);

append_index = num_threads;
do
{
    printf("im in the do while...\n");
    printf("append count is: %d\n", append_count); …
Run Code Online (Sandbox Code Playgroud)

c algorithm do-while

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

为什么while循环无限重复?

我想提示用户输入一个整数,但是如果用户输入一个非整数,程序应该一直要求一个整数,直到用户符合.

int getInteger(){
    int input;

    do{
        std::cout << "Enter an integer: ";
        std::cin >> input;
    } while(!(std::cin));


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

如果用户输入一个整数,该函数将返回它.

但是如果用户输入类似"Hello"的内容,则该函数继续无限,cout"输入一个整数:".

我该如何解决?

c++ loops std cin do-while

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

!操作员在做循环时

我只是在C中进行插入排序的教程,我很难理解它的方式!运算符用作do while循环的条件.

条件只是while (!done)但我不明白它是如何知道什么不应该等于变量完成.喜欢的东西while (done==0)会让我更有意义.

我只是开始编码,如果答案非常明显,那就很抱歉!我试过谷歌搜索但我发现很难表达问题...

代码如下:

void insertionSortArray (int arr[])
{
    for(int i = 1; i < SIZE; i++)
    {
        int value = arr[i];
        int j = i -1;
        int done = 0;
        do
        {
            if (arr[j]>value)
            {
                arr [j+1] = arr[j];
                j - -;
                if (j<0)
                done = 1;
            }

            else
                done = 1;

        } while (!done);

        arr [j+1] = value;
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢!!

c insertion-sort do-while

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

输入负数后循环不终止

我正在编写一个应该在正整数列表中读取的程序(包括0).如果用户输入的数字超过100或输入负数,则该循环应该终止.

我尝试做一个简单的do-while循环来解决这个问题但是在运行时,无论我提供什么数字,循环都永远不会终止.我试过几次改变循环,到目前为止都没有成功.我一定是犯了一个非常微妙的菜鸟错误,但我似乎无法抓住它.

    int[] list = new int[100];
    int size=0;
    do{
        list[size] = kbd.nextInt();
        size++;
    }while((size < 100) && (list[size] >= 0));


}
Run Code Online (Sandbox Code Playgroud)

java loops do-while

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