标签: while-loop

不确定我的while循环中发生了什么

我对编程和学习基础知识完全陌生.我无法弄清楚我的输出发生了什么.请帮忙.

这是我的代码(在mac上使用xcode)

// C Programing
// Program 6C: Basic While loop Example 2
// In this program we will demonstrate the use of basic while loop

#include <stdio.h> 


int main()
{

int values = 1;
int count = 1;
int value_array[values];

printf("Please enter how many values you have:  ");
scanf("%d", &values);

printf("You entered %d values.\n",values);

while (count <= values)
{
printf("Please enter your %d\n value:   ", count);
scanf("%d", &value_array[count]);
count++;
}

count = 1;

while (count <= values)
{
    printf("For …
Run Code Online (Sandbox Code Playgroud)

c arrays scanf while-loop

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

tkinter root.mainloop与While True循环

我正在使用tkinter来显示一些基于所读取电压的标签。但是,它在读取一次后停止执行。我发现这是由于root.mainloop()引起的。但是我无法修复它。我已经包含了我的代码。root.mainloop()位于True末尾。

from Tkinter import *
import spidev
import time
import os
import RPi.GPIO as GPIO
import numpy

GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
adcvolt = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
batvolt = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

root = Tk() #Makes the window
w,h = root.winfo_screenwidth(), root.winfo_screenheight()
root.overrideredirect(1)
root.geometry("%dx%d+0+0"%(w,h))

labelFont = ("times",15 ,"bold", "italic")
labelText1 = StringVar()
labelText2 = StringVar()
labelText3 = StringVar()
labelText4 = StringVar()
labelText5 = StringVar()
labelText6 = StringVar()

labelText1.set("Battery Management System")
label1 = Label(root, textvariable=labelText1, height=3, anchor="center")
label1.config(font=labelFont) …
Run Code Online (Sandbox Code Playgroud)

python user-interface tkinter while-loop

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

循环时Python缩进错误

while x <= 9:
    result = usertype()
    if result == 'Correct':
Run Code Online (Sandbox Code Playgroud)

我在"result = usertype()"的结果"t"上收到缩进错误.有人能解释一下吗?

*编辑我试过重写它,我检查确保所有的缩进确实是缩进而不是空格.现在变得非常沮丧和困惑.我认为它可能是导致问题的前一行中的某些内容.

*编辑2从命令提示符复制错误,因为IDLE对我不起作用:

  File "<stdin>", line 5
    result = usertype()
         ^
IndentationError: expected an indented block
>>>         if result == 'Correct':
  File "<stdin>", line 1
    if result == 'Correct':
    ^
IndentationError: unexpected indent
>>>             x = x + 1
  File "<stdin>", line 1
    x = x + 1
    ^
IndentationError: unexpected indent
>>>             y = y + 5
  File "<stdin>", line 1
    y = y + 5 …
Run Code Online (Sandbox Code Playgroud)

python syntax indentation syntax-error while-loop

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

Python while循环添加错误

好吧所以我几天前几乎没有上课,对我来说一切都是新的.我想要做的是创造一些东西,将摄氏温度转换为华氏温度和开尔文,我已经做到了,虽然它有点草率.

无论如何我的问题是累加器.当我得到所有摄氏度的总和时,我得到231当它应该是210.它增加了21我从"摄氏度<21.

任何人都可以帮我弄清楚如何获得正确的数字:/?谢谢!

print ("Celsius Conversion Chart")


sum = 0
sum1 = 0
sum2 = 0
celsius = 0

while celsius < 21:
    print (str(celsius)+'°C ' + ' = ' + str(celsius*9/5+32)+'°F ' + ' = ' + str(celsius +273.15)+'°K')
    celsius += 1
    sum = sum + celsius
    sum1 = sum1 + (celsius * 9/5 + 32)
    sum2= sum2 + (celsius + 273.15)

print (str(sum1)+'°F')
print (str(sum2)+'°K')
print (str(sum)+'°C')

if celsius < 100:
    print ("WARNING: temperature is too high")
Run Code Online (Sandbox Code Playgroud)

python loops while-loop

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

确定Big-o While循环

// n> 0

i ? 0 
while (i < n) 
     j ? 0 
     while (j < power(2,i)) 
         j ? j + 1 
     done 
    i ? i + 1 
done
Run Code Online (Sandbox Code Playgroud)

总体复杂度为O(n(log(n)),因为内部while循环具有条件,其中2 ^ i所以2 ^ 0 2 ^ 1 2 ^ 2 ... = 1 2 8 16 32 64 128 ......等那么2 ^ i <n - > log(n)> i?

外环看起来简直就是O(n).

O(n(log(n))的多个循环复杂度,请确认?提前感谢.

algorithm big-o while-loop

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

基本的循环概念

这是用于评估两个字符串的长度和字符是否相等的正确代码.

function equal(x, y) {
    if(x.length != y.length)
         return false
    var i = 0;
    while(i < x.length) {
       if(x.charAt(i) != y.charAt(i))
          return false
       i++
    }
    return true
  }

equal("hello", "hello") // true
equal("hello", "heyyy") // false
Run Code Online (Sandbox Code Playgroud)

我自己修改了它,我认为这对我来说是合乎逻辑的.但是我的代码(如下所示)在其角色内容不同时返回true.任何人都可以帮我解决我的版本有什么问题吗?

function equal(x, y) {
  if(x.length != y.length)
     return false
  var i = 0;
  while(i < x.length) {
     if(x.charAt(i) != y.charAt(i))
        return false
     else return true
    i++
   }
}

equal("hello", "hi") //false
equal("hello", "heyyy") //true
Run Code Online (Sandbox Code Playgroud)

javascript loops while-loop

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

使用read和IFS进行Bash while循环

我必须以下列格式解析文件:

line1_param1:line1_param2:line1_param3:
line1_param2:line2_param2:line2_param3:
line1_param3:line3_param2:line3_param3:
Run Code Online (Sandbox Code Playgroud)

并逐行处理,从当前行中提取所有参数.目前我以这样的方式管理它:

IFS=":"
grep "nice" some_file | sort > tmp_file
while read param1 param2 param3
do
  ..code here..
done < tmp_file
unset IFS
Run Code Online (Sandbox Code Playgroud)

如何避免创建临时文件?

不幸的是,这不能正常工作:

IFS=":"
while read param1 param2 param3
do
  ..code here..
done <<< $(grep "nice" some_file | sort)
unset IFS
Run Code Online (Sandbox Code Playgroud)

由于整行被分配给param1.

bash while-loop ifs

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

使用while(x)

#include<stdio.h>
#include<conio.h>

/* Function Declaration
int pal(int x); */
/*Declaring Second Function*/

int rev(int x);

int main()
{
    int a, b, c;
    clrscr();

    printf("Enter The Number Which You Want To Check Is Palindrome Or Not\n");
    scanf("%d", &a);

    b = rev(a);
    printf("%d", b);

    if(b == a) {
        printf("%d Is A Palindrome Number", a);
    } else {
        printf("%d Isn't A Plaindrome Number", a);
    }

    getch();
    return(0);
}

int rev(int x)
{
    int d = 0;

    while(x) {
        d = d * 10 …
Run Code Online (Sandbox Code Playgroud)

c while-loop

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

虽然循环没有Java的身体

while(或Java的任何其他循环)的主体可以是空的.这是因为null语句(仅由分号组成的语句)在Java中在语法上是有效的.现在考虑一下:

i = 100     

j = 200   

while(++i < --j); // no body in this loop  

  system.out.println(i);         

  system.out.println(j);      
Run Code Online (Sandbox Code Playgroud)

我注意到,如果i的值小于j,则循环重复自身,当条件未能满足时,则只有下面的2个语句被提供.我是正确的还是这背后还有其他逻辑吗?

java while-loop

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

为什么我的循环早期破裂?

我正在为课程编写一个功能,需要花费一些金钱,并告诉用户有多少硬币加起来.除了便士之外,我似乎一切都运转得很好.在添加适当数量之前,我的循环有时会停止并缩短.它通常会停止1便士,但有时它会给我正确的答案(我找到的一个值给出了正确的答案是.09).我已经尝试将浮动更改为双,我有同样的问题.我拉着我的头发试图找出我做错了什么.

void change(float total)
{
int quarters, dimes, nickels, pennies;
quarters = 0;
dimes = 0;
nickels = 0;
pennies = 0;

printf("\nTotal value entered: %.2f", total);

while (total >= .25)
{
    quarters += 1;
    total -= .25;
}
while (total >= .10)
{
    dimes += 1;
    total -= .10;
}
while (total >= .05)
{
    nickels += 1;
    total -= .05;
}
while (total >= .01)
{
    pennies += 1;
    total -= .01;
}
printf("\nQuarters: %d \nDimes: %d \nNickels: …
Run Code Online (Sandbox Code Playgroud)

c while-loop

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