标签: while-loop

R while循环和次数

有一个while循环,并且工作正常。但是,我还需要添加另一个条件。

我需要循环来继续直到它满足while循环,然后我还需要补充一点,即它只能重复x次。

我认为您必须进行一次for循环才能执行x次,是否可以在其中放置while循环?

基本上,如何使循环达到目标或在x循环后停止?

for-loop r while-loop

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

为什么这不会退出while循环?

我试图在外面退出,所以我的脚本可以发布到Wordpress.com博客.但是,即使我尝试使用休息; 在if语句中,循环继续.

此函数启动脚本并基本上处理发布:

function getFlogArticle($url, $mail) {  
list($id, $title, $content, $tags) = getNewArticle();
while ($id != 0)
{
    $start = getTime(); 
    doesArticleExist($url, $id);
        if ($exist = 0)
            {
                wordpress($title, $content, $mail, $tags, $url, $id);
                break;  
                $end = getTime(); 
                echo  '<strong>Exist While</strong>: '.round($end - $start,4).' seconds<br />'; 
            }
    list($id, $title, $content, $tags) = getNewArticle();   
    echo 'I cant stop'; 
}
}
Run Code Online (Sandbox Code Playgroud)

每次doARticleExist()返回1时,此函数都会从数据库中获取文章:

function getNewArticle() {
$start = getTime(); 
global $db;
$count = $db->query("SELECT * FROM flog_articles");
$count = $count->num_rows;
$offset = mt_rand(0, …
Run Code Online (Sandbox Code Playgroud)

php while-loop

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

循环时我无法弄清楚这一点

我对下面的代码有点麻烦,基本上我只是练习循环并考虑制作一个小游戏来刷...但是我有点难过为什么while循环让一个条目像"例如,cat"或"hgh",因此最后要警惕说出一堆乱码......

    var playerWeapon = prompt("Before you go to slay the Dragon, please choose your weapon from the following... \n\n1.) Sword \n2.) Crossbow\n3.) Dagger").toLowerCase();

    while( (playerWeapon == '' ) && (playerWeapon != 'sword' || playerWeapon != 'dagger' || playerWeapon != 'crossbow') ){

       alert("We need you to pick a weapon...");

       var playerWeapon = prompt("Sorry, you need to chose a valid weapon...what will it be? \n\n1.) Sword \n2.) Crossbow\n3.) Dagger").toLowerCase();
                }

    var weaponStrength = '';

    if(playerWeapon == "sword"){
        var weaponStrength = 10;
    } …
Run Code Online (Sandbox Code Playgroud)

javascript while-loop

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

退出一个循环Java

即使我将count变量设置为false,我也无法停止此循环.它不会转到Main类中调用的下一个方法.任何帮助将不胜感激.

主类

public class Main {


    public static void main(String[] args) {

        Var V = new Var();
        End E = new End();

        V.enter();
        E.end();

    }

}
Run Code Online (Sandbox Code Playgroud)

Var类

public class Var {

    static int x = 0;
    static boolean count = true;


    public static void enter(){

        while (count = true ){
            x = x+10;
            System.out.println(x);
            count = false;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

java loops if-statement while-loop

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

使用while循环的Fizz Buzz脚本卡在无限循环中 - php

我正在尝试使用while循环来编写Fizz Buzz脚本,以循环显示数字,1-100并将echo每个数字循环到屏幕.

我正在使用模数运算符来查找数字是否为以下数字的倍数:

  • 3在这种情况下它回声Fizz,
  • 5在这种情况下它回声Buzz,
  • 或者如果它是3和5的倍数,那么它就会回归FizzBu​​zz

我已经编写了下面的代码,测试了它的所有部分,它似乎工作,但是当我运行脚本时,它会陷入无限循环,回显Fizz.

$i = 1;

while ($i <= 100) {

    if ((3 % $i) === 0) {
        echo 'Fizz';
        $i = $i++;
    } else if ((5 % $i) === 0) {    
        $i = $i++;
        echo 'Buzz';        
    } else if ( ((3 % $i) === 0) && ((5 % $i) === 0)){
        echo 'FizzBuzz';
    } else {
        echo $i++;
    }

}
Run Code Online (Sandbox Code Playgroud)

有什么想法我错了吗?

php fizzbuzz while-loop

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

请解释此javaScript代码如何删除所有节点

HTML

<ul id="myList">
   <li>Item 1</li>
   <li>Item 2</li>
   <li>Item 3</li>
   <li>Item 4</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

我似乎无法理解这个while语句如何删除上面列表中的所有节点.有人可以解释一下吗?

JavaScript的

while(myList.firstChild) {
    myList.removeChild(myList.firstChild)
};
Run Code Online (Sandbox Code Playgroud)

html javascript html5 while-loop removechild

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

在while循环中改变条件的顺序会使程序崩溃

在while循环中改变条件的顺序会使程序崩溃.如果我这样写:while(a[in-1]>=temp && in>0),该程序不会工作.为什么?错误是:ArrayIndexOutOfBoundsException:-1.以下代码有效,直到我进行上述更改.

 public class ISort {
public int[] sort(int[] a){
    int n=a.length;
    for(int out=1;out<n;out++){
        int temp=a[out];
        int in=out;
        while(in>0 && a[in-1]>=temp ){
           a[in]=a[in-1];
           in--;
        }
        a[in]=temp;
    }
    return a;
}
public static void main(String[] args){
    int[] a={3,2,1,6,3,8,8,10,9};
    ISort s=new ISort();
    int[] res=s.sort(a);
    for(int i=0;i<res.length;i++ ){
        System.out.print(" "+res[i]+" ");
    }
}
}
Run Code Online (Sandbox Code Playgroud)

java loops for-loop while-loop object-oriented-analysis

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

在计算某个整数的除数的#时,我的程序无法突破'While Loop'?

当我在eclipse中运行程序时,在用户输入某个整数后继续运行并实际计算它所具有的除数的数量并用计数打印出来之后,我无法摆脱while循环.

   /*
 * This program reads a positive integer from the user. 
 * It counts how many divisors that number has, and then it prints the result.
 * Also prints out a ' . ' for every 1000000 numbers it tests. 
 */

import java.util.Scanner;

public class forloopTEST1 {


public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int N; //A positive integer entered by the user.
           //  Divisor of this number will be counted.

    int testDivisor;    // A …
Run Code Online (Sandbox Code Playgroud)

java for-loop if-statement while-loop integer-division

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

使用等于方法的否定

我正在尝试设置while循环,如下所示:

while(!data.equals("String");
Run Code Online (Sandbox Code Playgroud)

每当我运行此代码时,我都会收到NullPointerException.为什么是这样?

如果我将代码更改为:

while(data.equals("String");
Run Code Online (Sandbox Code Playgroud)

我没有这样的例外,所以数据中必须有数据,对吗?

编辑:在评论员的要求下添加真实代码.

以下代码是一种尝试将中缀表示法转换为后缀表示法的方法.

    public static Queue infixConvert (LinkedListTest infix){
    Stack stack = new Stack();
    Queue postfix = new Queue();

    while(infix.head.data != "EOF"){

        if(isNumber(infix.head.data)){
            postfix.insert(infix.head.data);
            System.out.println("Insert Queue");
            System.out.println("Operator");
        }

        else if (infix.head.data.equals("(") || infix.head.data.equals(")")){
            if(("(").equals(infix.head.data)){
                stack.push(infix.head.data);
                System.out.println("Open paren");
            }
            else {
                infix.delete(")");
                while(!"(".equals(stack.head.data)){
                    stack.delete(")");
                    postfix.insert(stack.pop());
                    System.out.println("Insert Queue");
                }
                stack.delete("(");
                System.out.println("Close Paren");
            }
        }

        else{
            if(!(highPrec(stack.head.data, infix.head.data))){
                stack.push(infix.head.data);
                System.out.println("Push onto Lesser Operand");
            }

            else if(highPrec(stack.head.data, infix.head.data)){
                while(stack.head.data != null){
                    if (stack.head.data != "("){
                        postfix.insert(stack.pop());
                    }
                    else break; …
Run Code Online (Sandbox Code Playgroud)

java equals nullpointerexception while-loop

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

php while循环只运行一次

我有一个PHP脚本来检查一些数据的可用性.我从外部jquery调用这个脚本.jquery运行正常.这是我的PHP:

<?php


$avares = checkAva($fi_nm, $tbl_nm, $txtval);

echo $avares;

function checkAva($field, $table, $curval) {

$avres = mysql_query("SELECT " . $field . " FROM " . $table . "") or die("query failed");

while ($a_row = mysql_fetch_array($avres)) {
    $dbval = $a_row[$field];

    if ($curval == $dbval) {
        return "no";
    } else {
        return "yes";
    }
}
}

?>
Run Code Online (Sandbox Code Playgroud)

$ curval是来自外部jquery的变量.我的问题是虽然数据库中有很多条目,但while循环似乎只运行一次.我用一个整数变量检查它,而while循环似乎只运行一次.你能帮我解决一下吗?

php mysql while-loop

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