标签: while-loop

Python While循环,也使用模和继续命令

尝试完成要求我执行以下任务的任务:

“编写一个while循环,该循环计算1到20(含)之间的整数之和,不包括那些被3整除的整数。(提示:您会发现模运算符(%)和continue语句很方便。)

我尝试自己构造代码,但是对代码的评估超时。我猜我的语法不正确并导致无限循环

    total, x = 0, 1
    while x >=1 and x <= 20:
        if x%3==0:
            continue
        if x%3 != 0:
            print(x)
            x+=1
            total+=1
    print(total)
Run Code Online (Sandbox Code Playgroud)

预期的答案应该是:

20 19 17 16 14 13 11 10 8 7 5 4 2 1

但是我只是收到“超时”错误

***最新::

尝试这样做:

total, x = 0, 1
while x>=1 and x<=20:
    if x%3 == 0:
        x+=1
        continue
    if x%3 != 0:
       print(x)
       x+=1
       total=+1
print(total)
Run Code Online (Sandbox Code Playgroud)

收到此::

Traceback (most recent call last):
   File "/usr/src/app/test_methods.py", line 23, in test
    self.assertEqual(self._output(), …
Run Code Online (Sandbox Code Playgroud)

python continue modulo while-loop

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

从在异步方法内启动的while循环退出

如何打破异步方法内部的while循环。请参考下面的代码

我尝试了很多方法,但没有一个对我有用

async void TotalTimer(string time)
{
    while (true)
    {
        TimeSpan timesp = DateTime.Now - DateTime.Parse(time);
        TotalTime = timesp.Hours + " : " + timesp.Minutes + " : " + timesp.Seconds;
        await Task.Delay(1000);

        if (string.IsNullOrEmpty(time))
        {
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要停止并退出循环


更新的代码:

async Task TotalTimer(CancellationToken token)
{
    var intime = await App.Database.GetRecentIn();
    InTime = DateTime.Parse(intime.datetime).ToString("hh:mm tt");


    while (!token.IsCancellationRequested)
    {
        TimeSpan timesp = DateTime.Now - DateTime.Parse(intime.datetime);
        TotalTime = timesp.Hours + " : " + timesp.Minutes + " : " + timesp.Seconds; …
Run Code Online (Sandbox Code Playgroud)

c# asynchronous while-loop

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

从while循环返回而不退出python

我知道这是不可能的。返回将退出它。有没有办法使之成为可能。我有while循环,它计算值。我想从while循环返回该值,并将其用于进一步处理,然后再次返回while循环,并在停止的地方继续。我知道返回会退出循环。如何使其成为可能。

这是示例代码:

import datetime
import time
def fun2(a):
    print("count:", a)
def fun():
    count = 0
    while 1:
        count = count+1
        time.sleep(1)
        print(count)
        if count == 5:
            return count
a = fun()
fun2(a)
Run Code Online (Sandbox Code Playgroud)

我的输出:

1
2
3
4
5
count: 5
Run Code Online (Sandbox Code Playgroud)

要求的输出:

1
2
3
4
5
count: 5
6
7
8
9
and goes on....
Run Code Online (Sandbox Code Playgroud)

python for-loop while-loop python-3.x

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

做有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
查看次数

使用指针写数组元素的问题

第一个循环是完美的。第二不起作用。“ Z”怎么了?为什么循环不停止?当我在表中输入“ Z”时,循环不响应。

#include <iostream>
using namespace std;

int main()
{
    char tab[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G',
                'H', 'I', 'J', 'K', 'L', 'M', 'N',
                'O', 'P', 'Q', 'R', 'S', 'T', 'U',
                'V', 'W', 'X', 'Y', 'Z' };

    char *wsk = tab;

    while(*wsk <= 'Y')
    {
        cout << *wsk << " ";
        wsk++;
    }
    cout << endl;

    wsk = tab;
    while(*wsk <= 'Z')
    {
        cout << *wsk << " ";
        wsk++;
    }
    cout << endl;
}

Run Code Online (Sandbox Code Playgroud)

c++ pointers loops while-loop do-while

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

如何在while循环中检查事件?

我正在尝试创建一个可以调用一个函数的系统,该函数将等到按下某个键后该函数才结束,因此暂停程序直到按下下一个键。

我尝试在while循环中使用变量,但是,如果我尝试在keydown事件上更改该变量,则在循环运行时它不会改变。

    var x = true;
    myFunction (event) {
        x = false;
    }
    document.addEventListener("keydown", myFunction);
    function wait () {
        x = true;
        while (x);
    }
    wait();
Run Code Online (Sandbox Code Playgroud)

这不会返回任何错误消息,但是,程序在敲键后再也不会继续。

javascript events while-loop

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

无限while循环会导致系统崩溃吗?

int main(){
    while(1)  
    {
        //code
    }
    return 0; 
}
Run Code Online (Sandbox Code Playgroud)

这是一个无限循环,但这会导致系统崩溃吗?
它的表现如何?
我认为它将耗尽所有堆栈部分。

c infinite-loop while-loop

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

如何执行while循环给定次数?

我有这个while循环来获取下一个工作日(节假日和星期日除外)。但它是通过加上1天来计算的。我希望该天数由用户确定。我从下面的TextBox(TboxIzin)获取输入。

如何执行while循环来进行给定次数的计算?


         int i = 1;
         int sayi;
         IzinIslem i1 = new IzinIslem();
         int.TryParse(i1.TboxIzin.Text, out sayi);
Run Code Online (Sandbox Code Playgroud)
            public static DateTime GetNextWeekDay(DateTime date, 
                 IList<Holiday> holidays, IList<DayOfWeek> weekendDays)
        {
            int i = 1;
            int sayi;
            IzinIslem i1 = new IzinIslem();
            int.TryParse(i1.TboxIzin.Text, out sayi);

            // always start with tomorrow, and truncate time to be safe
            date = date.Date.AddDays(1);

            // calculate holidays for both this year and next year
            var holidayDates = holidays.Select(x => x.GetDate(date.Year))
                .Union(holidays.Select(x => x.GetDate(date.Year + 1)))
                .Where(x => x …
Run Code Online (Sandbox Code Playgroud)

c# while-loop winforms

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

用-O3编译时的不同评估

当使用g++ -O3 ..和不使用时,以下代码具有不同的行为-O3。

我知道这updateError不会返回任何东西,这可能导致不确定的行为。我不明白的是,error如果使用-O3 if(error > eps)进行编译,则在循环内将其设置为零,并且被评估为false,而在while()语句中仍被评估为true 。。

我还尝试将其更改为do {} while()循环,在这种情况下,它也可以与-O3一起使用...

#include <stdio.h>


using namespace std;


double updateError(){
    printf("updating Error\n");
}

int main(int argc, char *argv[]){

    double eps = 1.e-10;
    double error = 2*eps;
    int iter = 0;

    while(error > eps){
        error = updateError();
        iter++;
        printf("Error on iteration %i: %.20e\n", iter, error);
        if(error > eps) printf("error bigger\n\n");
        else printf("error smaller\n\n");
        if(iter == 5){
            printf("not converged in 5 iterations!!\n");
            break; …
Run Code Online (Sandbox Code Playgroud)

c++ optimization evaluation gcc while-loop

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

循环中的几个语句

我正在尝试放入另一条语句以逻辑AND结束while循环:

while ( (character != '\n') && ( i < 10 ) )
Run Code Online (Sandbox Code Playgroud)

...多余的+10:th个字母不会存储在数组中。但是我仍然可以输入输入,直到按ENTER /'\ n'为止。

我究竟做错了什么?“额外输入”会发生什么?

这是代码:

char character;
char buffer[81];
int i = 0;

do
{
    character = getchar();
    buffer[i] = character;
    ++i;
} while ( (character != '\n') && (i < 10) );

buffer[i-1] = '\0';
Run Code Online (Sandbox Code Playgroud)

c while-loop logical-operators

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