标签: while-loop

php,while()循环中的无限循环

/// infinite loop??
$x=1; 
while($x=9){ 
echo $x;
$x++;
}
Run Code Online (Sandbox Code Playgroud)

我不明白背后的原因,为什么上面的代码导致无限循环在我看来上面的代码应输出"9"一次.但它输出无尽的999999999 ......

首先(当x等于1时)虽然语句是假的,所以没有任何反应,然后x变为2但是当语句为假时;

因此,当x变为9而while语句为true所以它应该回显9然后我们因x ++而加1; 它变成10,所以虽然声明变得虚假但是因为我看到它不是因为

它继续回响9999999 .......

关于上面的代码,请告诉我.最好的祝福.

注意:我已经检查了类似的问题,但无法找到我的情况的答案thx

php infinite-loop while-loop

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

.eof()循环不起作用

我正在尝试从文件中读取数字并将它们放入数组中.现在,当我运行该程序时,它打印8个数字,然后该行结束并打印相同的8个数字.这是一个永无止境的循环.我究竟做错了什么?

#include <iostream>                                                             
#include <fstream>                                                              
using namespace std;                                                            

int main()                                                                      
{                                                                               
  int num;                                                                      
  ifstream infile;                                                              
  infile.open("euler8Nums.txt");                                                
    infile >> num;//must attempt to read info prior to an eof() test            
    while(!infile.eof()){                                                       
      cout << num << endl;                                                      
      infile >> num;                                                            
    }                                                                           
    infile.close();                                                             
    return 0;                                                                   
}  
Run Code Online (Sandbox Code Playgroud)

c++ loops while-loop

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

在java实验室中使用While(用户输入无效输入)

我在上课的一部分实验室遇到了一些麻烦,大部分都很容易,但由于某种原因,我的声明并没有按照我的意愿行事.它应该返回无效输入,然后提示用户租用; 但是,它认为每个输入都无效.(应该很容易在代码中看到)我该如何解决这个问题?或者我可以找到这些信息,因为我无法在我的书中找到它.(第二个while循环,第一个工作)

import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class lab8
{
    public static void main (String[] args)
    {
        int choice;
        String item;
        double costper;
        ArrayList <String> Items = new ArrayList <String> (); 
        Items.add ("computer");
        Items.add ("Monitor");
        Items.add ("Color Printer");
        Items.add ("Color Scanner");
        Items.add ("DVD/CDROM");
        Items.add ("To Quit");

        ArrayList <Integer> Num = new ArrayList <Integer> ();
        Num.add (0);
        Num.add (1);
        Num.add (2);
        Num.add (3);
        Num.add (4);
        Num.add (5);

        System.out.println ("\t\tMy Super Computer Store\n");
        int index=0;
        int endex= 0;
        while (index …
Run Code Online (Sandbox Code Playgroud)

java while-loop

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

Java while循环问题

我有一个没有执行的while循环.我不相信存在无限循环或启动问题,但有吗?我找不到我的逻辑错误!

public class Test
{
    public static void main (String [] args)
    {
        int i = 1;
        int j = 1;
        while  ((i < 10) && (j*j != 25));
        { 
           i++;  
           ++j;
           System.out.println( i * j );
        }   
    }
}
Run Code Online (Sandbox Code Playgroud)

java while-loop

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

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

为什么会产生无限循环(C,While循环)

我正在为我的班级做一个程序,我需要在开始之前将数组'decade'的所有值设置为-1.我尝试了这个(和for循环中的版本)它只是让我进入一个无限循环!有人可以解释为什么会这样,以及我如何解决它?

码:

int decade[9][9], i = 0, k = 0;
while (i<10) {
    while (k<10) {
        printf("i is %d, k is %d\n",i,k);
        decade[i][k] = -1;
        k++;
    }
    k=0;
    i++;
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

它打印出来: 什么打印出来

对于任何需要未来答案的人来说,将十年称为"十年[10] [10]"而不是"十年[9] [9]",或者无论如何定义.

c loops infinite while-loop

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

Java,需要一个while循环来实现eof.iewhile!eof,继续解析

我目前有一个有效的解析器.它解析一次文件(不是我想要它做的),然后将解析后的数据输出到文件中.我需要它继续解析并附加到相同的输出文件,直到输入文件的末尾.看起来像这样.

try {
// my code parsing the data and appending to eof of output. (works)
}
catch (EOFException eof){
}
Run Code Online (Sandbox Code Playgroud)

除了while循环外,一切都已完成.它只在我需要时解析一次才能继续解析.我正在寻找一个while循环函数来达到eof.

我也在使用DataInputStream.是否有某种DataInputStream.hasNext函数?

DataInputStream dis = new DataInputStream(new FileInputStream(inFile));
i.e. dis.read();
Run Code Online (Sandbox Code Playgroud)

.

//Need a while !eof while loop
try {
// my code parsing the data and appending to eof of output. (works)
}
catch (EOFException eof){
}
Run Code Online (Sandbox Code Playgroud)

java while-loop eof datainputstream

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

使用带有while循环的字典

我试图在while-loops条件语句中使用字典键,程序将不会将任何输入识别为"正确"

class Gymnast_room(KeyCode):    
    def enter(self):
        print "This is the gymnastics room, we got foam pits and rings!"
        print "Before you can enter, you have to enter the code"
        guess = int(raw_input('What is the code? >>'))

        passcodes = {'Gymnast_code': 1234,
        'Powerlifting_code': 1234
        }
        while guess != passcodes['Gymnast_code']:
            guess = int(raw_input('What is the code? >>'))
            if guess == passcodes['Gymnast_code']:
                print "Correct!"
                return Powerlifting_room()
            else:
                print "Incorrect!"
Run Code Online (Sandbox Code Playgroud)

python dictionary while-loop

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

Perl:如何打破嵌套循环?

我在Perl中遇到了一个问题.我想用Regex扫描巨大的文本文件.一切正常,但是当我在嵌套的while循环中找到Stop Codon时,第一个while循环接管,但它应该从找到的终止密码子的最后一个位置继续.有人能给我一个暗示在哪里看?

我已经尝试了最后一个声明,但这不起作用.

use strict;
use warnings;
$|=1;

sub read_Fasta {
    my ($filename) = @_;
  open (FIN, "<$filename") or die "Bestand $filename niet te openen\n";
  my $sequence = "";
  while (my $line = <FIN>) {
      if ($line =~ /^\s*$/) {
      next;
    } elsif ($line =~ /^\s*#/) {
      next;
    } elsif ($line =~ /^>/) {
      next;
    } else {
      $sequence .= $line;
    }
  }
  close (FIN);
  $sequence =~ s/\s//g;
  return ($sequence);
}

print "Het vinden van ORF's in een organisme.\n\n";
print …
Run Code Online (Sandbox Code Playgroud)

perl loops while-loop

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

"使用未分配的局部变量"错误C#,"while"命令

在编写代码时,我收到了a,b和c的错误.

           while(n==0)
        {
            a = Convert.ToInt32(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            c = Convert.ToInt32(Console.ReadLine());
            n=CheckTriang(a, b, c, n);

        }

        x=RightTriang(a, b, c, x);
Run Code Online (Sandbox Code Playgroud)

我在循环中输入a,b和c的值,然后在工作方法CheckTriang中检查它们(确定值是否可以生成有效的三角形),并打破条件,返回n = 1.我认为问题可能在于编译器不确定循环是否会停止给出值.如何重写代码更清晰?

提前致谢!

编辑:我只复制了有问题的部分,而不是整个代码.声明了所需的变量.

c# while-loop

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

标签 统计

while-loop ×10

loops ×4

java ×3

c++ ×2

c ×1

c# ×1

datainputstream ×1

dictionary ×1

eof ×1

for-loop ×1

infinite ×1

infinite-loop ×1

perl ×1

php ×1

python ×1