标签: while-loop

循环中的PHP - 没有停止

我正在使用php while循环来读取结果,但它只是在第一条记录上不断循环,从不移动到第二条记录而且永远不会停止.我的代码:

class MySqlDatabase {   

    public function __construct() {
        $this->Open_Connection();
    }

    //Vars for Function Open_Connection
    private $_Connection;
    //Function Open_Connection - Connects to DB
    public function Open_Connection(){
        $this->_Connection = mysqli_connect('localhost','xxx','xxx','xxx');
        if(!$this->_Connection){
            die("Connection Failed: ". mysqli_error($this->_Connection));
        } else {
            echo "Success";
        }
    }

    //Vars for Function Query
    public $Results_Row;
    //Function Query - Runs Query and returns results 
    public function Query($Sql){
        $Results = mysqli_query($this->_Connection, $Sql);
        if(!$Results){
            die("Query Failed: ". mysqli_error($Results). "<br/>".$Sql);
        }
        $this->Results_Row = mysqli_fetch_row($Results);

        return $Results;
    }

}

$Db = new MySqlDatabase; …
Run Code Online (Sandbox Code Playgroud)

php while-loop

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

计算在Android中单击按钮的时间(秒)

我想制作一个程序,告诉我按住按钮的秒数.

例如,如果我点击按钮5秒钟,它应该告诉我按钮保持按下5秒钟.

这在Android中是否可以实现?

这就是我尝试的方式,没有用,因为当我把while循环放入时,按钮保持点击状态,即使按下它只需一秒钟.该程序没有崩溃,但按钮仍然被点击,我认为它冻结了.

 thread = new HandlerThread("");



    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            while(btn.isPressed())
            {

                try {
                    thread.sleep(1000);
                    total = total++;
                    temp = temp++;
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            Toast.makeText(MainActivity.this, ""+temp+" "+total, 0).show();


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

multithreading android count while-loop buttonclick

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

循环中的分段错误,无法找到错误

我有以下代码完全正常,直到实现while循环,它给我一个分段错误,我无法找到它的位置.

#include <stdio.h>     /* for printf */
#include <stdlib.h>
#include <string.h>

int main (int argc, char **argv) {
    char str[200];
    char *tok = NULL;
    char path[300];

    strcpy(str, "a/b/c/d");

    printf("string before strtok(): %s\n", str);
    #string before strtok(): a/b/c/d
    tok = strtok(str, "/");
    strcpy(path,tok);
    printf("Current Tok: %s\n", tok);
    #Current Tok: a
    strcat (path,"/");
    printf("Current Path:%s\n",path);
    #Current Tok: a/

    while (1){
    strcat(path, "/");
    tok = strtok(NULL, "/");
    strcat(path, tok);
    printf("Path after strcpy:%s\n",path);
        if (tok == NULL){
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试手动完成,每一步都做得很好.

c while-loop

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

否则如果不在PHP工作

这是我的编码

          <?php
                try
                {
                    $stmt = $conn->prepare("SELECT * FROM registration");
                    $stmt->errorInfo();
                    $stmt->execute();

                    while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
                       {
                        echo "<tr>";
                        echo "<td>". $row['Autoid'] ."</td>";
                        echo "<td>". $row['Name'] ."</td>";
                        if($_SESSION['sesuname'])
                         {
                            echo "<td>Its You ". $_SESSION['sesuname'] ."</td>";
                         } 
                        else if(!$_SESSION['sesuname'])
                         {
                             echo '<td>Follow</td>';
                         }
                        echo "</tr>";
                       }
                } catch (PDOException $e) {
                       'Database Error : ' .$e->getMessage();
                }
            ?>
Run Code Online (Sandbox Code Playgroud)

我正在尝试显示Follow选项并在最终中登录用户名td.但是,此编码始终显示所有行中的登录用户名.我不知道我哪弄错了?

php mysql if-statement while-loop

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

Java while循环布尔评估

我不确定我是否理解这个循环

boolean b = false;
while(!b) {
System.out.println(b);
b = !b;
}
Run Code Online (Sandbox Code Playgroud)

它返回false,循环执行一次

但while(!b)确定了b= true吗?喜欢!b = !false和b打印出来?

java boolean-expression while-loop

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

为什么这是一个永无止境的循环?

用作输入的文件是.txt文件,其中包含:

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

它正在我的代码的第3行打开.然而while循环永远不会终止,不断地将"Pass"打印到控制台,那么为什么它不能infile.eof()在第5行文本之后找到?

int main(int argc, char** argv) {
    ifstream infile;
    infile.open("TEST.txt", ios::in);


    if(infile.is_open()){
        while(!infile.eof()){
            cout << "Pass" << '\n';
        }
    }
    else{
        cout <<"Fail";
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ loops if-statement while-loop

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

在变量名后自动添加数字,在一个条件下不添加?

怎么做:

$user1_rating=0;
$user2_rating=0;
$user3_rating=0;
$user4_rating=0;
$user5_rating=0;
$user6_rating=0;
$user7_rating=0;
$user8_rating=0;
$user9_rating=0;
$user10_rating=0;
Run Code Online (Sandbox Code Playgroud)

自动使用php(我会在while循环中使用它),而不是$ username == $ row ['username']与之前$ row ['username']相同?

php numbers while-loop

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

在此代码段中使用do-while循环有什么意义?

我刚刚发现Roslyn源代码,我看到一个代码片段如下所示:

var text = stringBuilder.ToString();
int length;
do
{
    length = text.Length;
    text = text.Replace("{\r\n\r\n", "{\r\n");
} while (text.Length != length);
Run Code Online (Sandbox Code Playgroud)

它看起来很奇怪,因为该String.Replace方法一次性用一个新值替换所有出现的指定字符串.所以这个循环首次执行,执行更改然后再次执行第二次并且什么都不做.然后它结束..那么使用a loop而不是只写的重点是什么:

var text =  stringBuilder.ToString();
text = text.Replace("{\r\n\r\n", "{\r\n");
Run Code Online (Sandbox Code Playgroud)

甚至更短:

var text = stringBuilder.ToString().Replace("{\r\n\r\n", "{\r\n");
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

c# loops while-loop do-while

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

使用循环内的函数创建随机字符串返回奇怪的输出(PHP)

我希望这个问题没有在另一个线程中得到解决 - 做了一些搜索但却一无所获.

我正在编写一个函数来以字符串的形式创建一些随机注释.它将用于生产中的循环上下文中.我写了一个while循环来测试函数,我得到了一些奇怪的输出.它在第一个循环中运行良好,但每个后续循环都将字符串截断为它们的第一个字符.

<?PHP

$prefix=array();
$prefix[]="Wow! That's";
$prefix[]="That's";
//...
$prefix[]="Amazing image. So";
$prefix[]="Wonderful image. So";


$suffix=array();
$suffix[]="amazing";
$suffix[]="awesome";
//...
$suffix[]="fabulous";
$suffix[]="historic";


$punctuation=array();
$punctuation[]='!';
$punctuation[]='!!';
//...
$punctuation[]='.';
$punctuation[]='...';

function comment() {
        global $prefix;
        $prefix_max=count($prefix)-1;
        $rand=rand(0,$prefix_max);
        $prefix=$prefix[$rand];

        global $suffix;
        $suffix_max=count($suffix)-1;
        $rand=rand(0,$suffix_max);
        if(strpos(strtolower($prefix),strtolower($suffix[$rand])) > 0) {
                $rand=$rand+1;
                if($rand > $suffix_max) {
                        $rand=0;
                }
        }
    $suffix=$suffix[$rand];

        if(substr($prefix, -1) == '.' || substr($prefix, -1) == '!') {
                $suffix=ucfirst($suffix);
        }


    $rand=rand(1,100);
        if($rand < 18) {$suffix=strtoupper($suffix);}

        global $punctuation;
        $punctuation_max=count($punctuation)-1;
        $rand=rand(0,$punctuation_max);
        $punctuation=$punctuation[$rand];

        $comment=$prefix.' '.$suffix.$punctuation;
        return $comment;
} …
Run Code Online (Sandbox Code Playgroud)

php loops function while-loop

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

Java while循环永远不会进入循环

我正在用java写一个视频扑克,我需要问玩家是否要从他们手中取出任何牌.我为此写了一个while循环,但它没有像现在这样的方式工作.如果有人能把我送到正确的方向,我会很感激 - 我还是java的新手......谢谢.(这counter是因为玩家不会删除超过5张牌)

    String response = "y";
    int counter = 0;
    System.out.println("Remove any cards?");
    System.out.println("Enter y for 'yes' and n for 'no'");
    response = input.nextLine();
    while((response != "n") && (counter < 5))
    {

        System.out.println("Enter the number of a card to be removed (1-5)");
        int l = input.nextInt();
        p.removeCard(p.getHand().get(l-1));
        p.addCard(cards.deal());
        cards.incrementTop();
        counter ++;
        System.out.println("Card removed. More? Type 'yes' or 'no'");
        String answer = input.nextLine();
        if (answer == "no")
            {
                response = "n";
            }       
    }  
Run Code Online (Sandbox Code Playgroud)

java while-loop

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