标签: while-loop

PHP循环中的交替颜色行

如何在我的php循环中有一个交替的颜色行?

$num = mysql_num_rows($qPhysician);

$i=0;

while($i < $num)

{

    echo "<tr>";
    echo "<td>" . mysql_result($qPhysician,$i,"lastName") . "</td>";
    echo "<td>" . mysql_result($qPhysician,$i,"firstName") . "</td>";
    echo "</tr>";

    $i++;

}
Run Code Online (Sandbox Code Playgroud)

我必须省略"tr"和"td"的"<"和">",因为在这个问题中不允许这样做.:)

谢谢!

php rows while-loop alternating

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

Bash在读取文件的循环内读取

我正在编写一个脚本,从csv文件中提取数据,操纵数据,然后询问用户更改是否正确.问题是您似乎无法在正在读取文件的while循环中执行读取命令.下面包含一个测试脚本,注意如果没有真正使用它,则需要创建一个in文件.这只是我正在处理的更大脚本的摘录.我正在重新编码它以使用似乎有效的数组,但想知道是否有任何解决方法?我一直在阅读几个bash指南,以及阅读的手册页,但没有找到答案.提前致谢.

#!/bin/bash
#########
file="./in.csv"
OLDIFS=$IFS
IFS=","
#########

while read custdir custuser
do
    echo "Reading within the loop"
    read what
    echo $what
done < $file

IFS=$OLDIFS
Run Code Online (Sandbox Code Playgroud)

bash while-loop

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

Python"While"循环逻辑错误?

我有一个Python脚本,每5秒查询一次MySQL数据库,收集帮助台票证的最新三个ID.我使用MySQLdb作为我的驱动程序.但问题是在我的"while"循环中,当我检查两个数组是否相等时.如果它们不相等,我会打印出"新票已到达".但这永远不会打印!看我的代码:

import MySQLdb
import time

# Connect
db = MySQLdb.connect(host="MySQL.example.com", user="example", passwd="example", db="helpdesk_db", port=4040)
cursor = db.cursor()

IDarray = ([0,0,0])
IDarray_prev = ([0,0,0])

cursor.execute("SELECT id FROM Tickets ORDER BY id DESC limit 3;")
numrows = int(cursor.rowcount)
for x in range(0,numrows):
   row = cursor.fetchone()
   for num in row:
      IDarray_prev[x] = int(num)
cursor.close()
db.commit()

while 1:
   cursor = db.cursor()
   cursor.execute("SELECT id FROM Tickets ORDER BY id DESC limit 3;")

   numrows = int(cursor.rowcount)
   for x in range(0,numrows):
      row = cursor.fetchone()
      for num in …
Run Code Online (Sandbox Code Playgroud)

python logic while-loop

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

Delphi - 如何使用启动/停止功能以毫秒或纳秒为单位制作定时器?

我在Delphi7中寻找一个以毫秒或纳秒为单位的计时器.我必须通过顺序搜索检查三个ISAM文件的速度.第一个ind文件包含50个字符串,如"record_0"到"record_50".第二个 - "record_0"到"record_500",第三个 - "record_0"到"record_5000".我实现了一切,但我不知道如何制作计时器.我正在比较一个字符串与每个ISAM文件中的最后一项.这是我的第一个ind文件的代码:

procedure TForm1.Button1Click(Sender: TObject);
  var i:integer;
  var content : String[20];
  var indexCounter:integer;
  var keyword:string;
begin
  //First ISAM file
  AssignFile(indF1, 'index1.ind');
  ReWrite(indF1);
  Reset(indF1);
  for i:=0 to 49 do begin
    content := 'record_';
    content := content + IntToStr(i+1);
    index1.index1 := content;
    index1.position1 := FileSize(indF1);
    Seek(indF1, FileSize(indF1));
    write(indF1, index1);
  end;
  CloseFile(indF1);
  Label12.Caption := FileSizeStr('index1.ind');

  //Sequential search in first ind file
  Reset(indF1);
  keyword := 'record_50';
  indexCounter := 0;
  //start timer
  while not Eof(indF1) do begin
    Seek(indF1, indexCounter);
    Read(indF1, Index1);
    if …
Run Code Online (Sandbox Code Playgroud)

delphi timer while-loop milliseconds

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

在Python中打破while循环后的流控制

我对编程和Python都很陌生.几次,我创造了一个感觉就像一个尴尬的程序流程,我想知道我是否遵循最佳实践.这在概念上是我想要做的:

def pseudocode():
    while some condition is true:
        do some stuff
        if a condition is met:
            break out of the while loop
    now do a thing once, but only if you never broke out of the loop above 
Run Code Online (Sandbox Code Playgroud)

我最终做了什么工作,但不知怎的感觉:

def pseudocode():
    while some condition is true:
        do some stuff
        if some condition is met:
            some_condition_met = True
            break out of the while loop

    if some_condition_met is False:
        do a thing
Run Code Online (Sandbox Code Playgroud)

有没有更好的办法?

python flow while-loop

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

从内部函数体内突破while循环

如果我有:

while(foo) {
  var x = bar.filter(function(){ /* any problems with using `break;` here to exit the while-loop? */ });
  bar(), baz();
  // and so on...
}
Run Code Online (Sandbox Code Playgroud)

会有任何奇怪或意外的行为吗?

javascript break while-loop

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

Python - 实现数值方程求解器(Newton-Raphson)

我警告你,这可能令人困惑,我写的代码更多的是思维导图而不是完成代码.

我正在尝试使用Newton-Raphson方法来求解方程.我无法弄清楚的是如何写这个

在此输入图像描述

Python中的等式,用于计算最后一次近似(xn)的下一个近似值(xn + 1).我必须使用循环,越来越接近真实的答案,并且当近似值之间的变化小于变量h时,循环应该终止.

  1. 如何编写等式的代码?
  2. 当近似值不再变化时,如何终止循环?

    计算方程f的导数,在点x,精度为h(这用于求解方程式())

    def derivative(f, x, h):
        deriv = (1.0/(2*h))*(f(x+h)-f(x-h))
        return deriv
    
    Run Code Online (Sandbox Code Playgroud)

    数值方程求解器

    假设循环直到近似值之间的差异小于h

    def solve(f, x0, h):
        xn = x0
        prev = 0
    
        while ( approx - prev > h):
             xn = xn - (f(xn))/derivative(f, xn, h)
    
        return xn
    
    Run Code Online (Sandbox Code Playgroud)

python while-loop numerical-methods

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

循环时无法访问代码

当我编译这段代码时

public static void main(String [] args) {

        int x = 0;        

        while(false)
        {                        
            System.out.println(hello);
        }
    }
Run Code Online (Sandbox Code Playgroud)

它显示编译时错误无法访问的代码.

但是,当我修改此代码时

public static void main(String [] args) {

        int x = 0;        
        boolean result = false;
        while(result)
        {                        
            x=4;
        }
    }
Run Code Online (Sandbox Code Playgroud)

它工作正常.

有人能告诉我这种行为背后的原因.

java while-loop

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

在循环中保存值

我正在尝试读取我的国家/地区数组字符串的值,该字符串读取csv文件.

InputStreamReader reader = new InputStreamReader(asset_stream);  
br = new BufferedReader(reader);
String[] country = null;
String cvsSplitBy = ";";

try {
    while ((line = br.readLine()) != null) {
        country = line.split(cvsSplitBy);

    }
} catch (NumberFormatException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

我的代码当前正在将值存储在country变量中.但是当我的循环结束时,我只在循环中读取了最后一个值.如何存储所有值,以便在完成循环后打印它们?

java split while-loop

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

获取数组长度时,在void上下文中无用的私有数组

我试图做一个循环时的长度停止@arr1和@arr2均为零.我收到了这条警告信息

Useless use of private array in void context
Run Code Online (Sandbox Code Playgroud)

当我用这个

while (scalar (@arr1, @arr2) ) {
  #more code
}
Run Code Online (Sandbox Code Playgroud)

为什么我会收到警告?

perl while-loop

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