标签: do-while

Do-While循环的可接受使用?

可能重复:
编码标准/编码C++中的最佳实践

我没有使用Do-While循环很多,但我发现自己在一个特定类别的情况下使用它做了一些逻辑,在那里我想要查看与多个退出点有关的事项列表但只能通过它一次.我总是对条件有"while(TRUE)"和"if(TRUE)break".在循环的最后,因为我只想通过它一次.[我猜或者我可以将这些2合并为"while(FALSE)."]

Do {

  // do some stuff

  if (!CheckCondition1) break;
  // do some stuff

  if (!CheckCondition2) break;
  // do some stuff

  ...

  // or delete this and change to while (FALSE);
  if (TRUE) break;
} while (TRUE);
Run Code Online (Sandbox Code Playgroud)

我一直在这些情况下使用Do-While结构,但我不确定它是否是一种"可接受"的方式来使用它或者是否有更好的方法.谢谢.

c c# c++ java do-while

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

JAVA - 使用FOR,WHILE和DO WHILE循环来加1到100

正如标题所述,我无法理解循环,并提出了一种简单的1到100总和的方法,但就像我说的那样,循环让我有些困惑.我想我有FOR循环.

这就是我想出来的.

int sum = 0; 
for (int i = 1; i <= 100; i++) sum += i; 
System.out.println("The sum is " + sum);
Run Code Online (Sandbox Code Playgroud)

java for-loop while-loop do-while

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

无限循环或早期终止循环

我一直在与这个项目争夺一段时间.它已经开始正确编译,但是当我按原样运行时,它会无限地说"你的账单是__".当我更改初始响应值时,它拒绝运行.我对如何修复它感到很失望.

/* Savannah Moore
   CSCI 1301
    2/11/2013
    Internet Package Code: Code determines customer's final pricing. */
import java.util.Scanner;

public class internet{
    public static void main(String[] args){
      double hours, price;
      int internet;
      String response, check="Yes";


      Scanner kb = new Scanner(System.in); /* This sections questions the user as to which package they have bought. */
       System.out.println("Our internet packages include:");
        System.out.println("Package 1: For $9.95 per month, 10 hours of access are provided. Additional hours are $2.00 per hour.");
        System.out.println("Package 2: For $13.95 per …
Run Code Online (Sandbox Code Playgroud)

java infinite-loop do-while

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

在Java中输入无效后重新提示用户

我正在用java编写这个程序,我需要在输入无效后重新提示用户.我找到了一个解决方案,但发现如果用户在重新提示后输入另一个无效输入,那么它会继续.有人可以告诉我一个更好的解决方案吗?无论如何我会告诉你我有什么:

System.out.println("What is your age?\n");
    age = userInput.nextInt();

    if((age > 120) || (age < 1)) {//error message
        System.out.println("ERROR Please enter a valid age");
        System.out.println("");
        System.out.println("What is your age?\n");
        age = userInput.nextInt();
    }//end if
Run Code Online (Sandbox Code Playgroud)

如果用户在再次提示后输入了无效输入,程序将继续,我该如何克服这个问题呢?

java loops for-loop while-loop do-while

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

Do-while仅在输入数字时继续

do
{
    try
    {
        a = Convert.ToDouble(Console.ReadLine());
    }
    catch
    {
        Console.WriteLine("Hodnota není ?íslo");
    }
} while (a != 'number');
Run Code Online (Sandbox Code Playgroud)

我需要在最后输入一个数字时结束循环,我不知道如何.有帮助吗?我是新来的.

c# console-application do-while

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

do - while循环,具有c中的多个条件

我试图做一个while-do循环退出当用户键入exit或退出时.由于某种原因,我无法理解我简单无法实现它.如果我单独应用其中一个条件它可以正常工作,但我需要同时具备这两个条件.

我在网上搜索,我发现了几个不同编程语言的例子,例如(PHP)Do-While循环多条件,(Python)如何进行while循环多条件,(C++)在do ... while循环中使用多个条件等但无论我遵循什么程序,我都能同时兼顾两种情况.

理想的解决方案是添加大写条件,但我可以在解决此问题后立即使用它.

也许一双新鲜的眼睛会看到我失踪的东西.

代码示例如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_CHARACTERS 250
#define MAX_USERNAME 12
#define MAX_USERS 1024
#define MIN_REQUIRED 3
#define MAX_PORT 65536
#define MIN_PORT 1

typedef struct rec {
  char username[MAX_USERNAME];
  char msg[MAX_CHARACTERS];
}RECORD;

/* Usage Instructions void because we do not have any return */
int help(void) {
  printf("Usage: test.c [-s <arg0>] [-s <arg1>]\n");
  printf("\t-s: a string program name <arg0>\n");
  printf("\t-s: a string with a Nickname maximum …
Run Code Online (Sandbox Code Playgroud)

c loops do-while

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

在C++中将FOR循环转换为DO WHILE循环

即时通讯试图将其转换为do-while; 它应该根据您输入的数字显示金字塔:

    int size,a,b,c;
    printf("Enter a Number: ");
    scanf("%d",&size);
    printf("\n");

    // for loop starts here
    for(a=1; a<=size; a++)
    {

     for(b=1; b<=size-a; b++)
     {
      printf(" ");
     }

     for(c=1; c<=(2*a)-1; c++)
     {
      printf("*");
     }

      printf("\n");
    }
     getch();
   }
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止所做的:

    int size;
    int a=1;
    int b=1;
    int c=1;

    do {

     a++;

      do {
       c++;
       printf("*");
      } while(c<=((2*a)-1));

        do {
       printf(" ");
          b++;
      } while(b<=(size-a));

      printf("\n");

    } while(a<=size);

    getch();
   }
Run Code Online (Sandbox Code Playgroud)

Aaaa和它没有显示相同的输出,任何暗示家伙?TIA :)

c++ loops for-loop do-while

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

意外标记"<'附近的语法错误

StudentAnwser=()
inputScriptFile=001.sh

while IFS= read -r line;
do
    StudentAnwser+=( "$line" )
done < <( sh $inputScriptFile test.txt )
Run Code Online (Sandbox Code Playgroud)

它返回一个错误

foo.sh: line 22: syntax error near unexpected token `<'
foo.sh: line 22: `  done < <( sh $inputScriptFile test.txt )'
Run Code Online (Sandbox Code Playgroud)

那有什么不对?我按照其他问题的解决方案从结果中读取行

bash shell do-while

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

while循环无条件地工作

据我所知,虽然声明需要条件才能工作,但在这里它没有任何作用; 怎么可能?while q:工作怎么样?

代码如下:......

q = set([])
for i in range(N):
    q.add((i, 0))
    q.add((i, M - 1))
    w[i][0] = h[i][0]
    w[i][M - 1] = h[i][M - 1]
for i in range(M):
    q.add((0, i))
    q.add((N - 1, i))
    w[0][i] = h[0][i]
    w[N - 1][i] = h[N - 1][i]

while q:
    ci, cj = q.pop()
    for ii, jj in ((0, 1), (0, -1), (1, 0), (-1, 0)):
        ni, nj = ci + ii, cj + jj
        if 0 <= ni …
Run Code Online (Sandbox Code Playgroud)

python while-loop do-while

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

做有3个条件的循环

我陷入了do-while循环的困境,该循环要求使用do-while循环,直到用户输入三个正确的字符串之一

我已经试过了

Scanner input = new Scanner(System.in);
    String motor = "motor";
    String mobil = "mobil";
    String kosong = "";
    String baru = "baru";
    int tahun = Calendar.getInstance().get(Calendar.YEAR);

do {
        inputVehicleType();
        vehicleCondition = input.next();
    }
while (!(vehicleCondition.equals(motor)) || (vehicleCondition.equals(mobil)) || (vehicleCondition.equals(kosong)));

System.out.println("SUCCED");

private static void inputVehicleType() {
    System.out.println(Constant.HEADER);
    System.out.println("Input Jenis Kendaraan Mobil/Motor --> (jenis [motor/mobil])");
    titleFooter();
}
Run Code Online (Sandbox Code Playgroud)

使用该语法,它只会检索(vehicleCondition.equals(motor)。我的预期结果是它可以检索(vehicleCondition.equals(motor),(vehicleCondition.equals(mobil),(vehicleCondition.equals(kosong))。

java while-loop do-while

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

标签 统计

do-while ×10

java ×5

while-loop ×4

for-loop ×3

loops ×3

c ×2

c# ×2

c++ ×2

bash ×1

console-application ×1

infinite-loop ×1

python ×1

shell ×1