标签: while-loop

PHP - 使用while循环创建嵌套数组

我想制作一个像这样的数组

$array = array("'firstName1', 'lastName1'", "'firstName2', 'lastName2'", ......);
Run Code Online (Sandbox Code Playgroud)

我总是得到这样的错误:

解析错误:第11行的C:\ wamp\www\Tests\index.php中的语法错误,意外'while'(T_WHILE),期待')'

<!doctype html>
<html>
<head>
    <meta charset="utf=8">
</head>
<body>
<?php
if(isset($_POST['reg'])){
    $x=1;
    $a = array(
    while($x<=10):
    "'firstName$x', 'lastName$x'"; //I DONT KNOW WHAT TO DO IN THIS LINE//
    $x++;
    endwhile;
    );
    print_r($a);
}
?>
<form action="" method="post">
<input type="text" name="number" /> <input type="submit" name="submit" value="Submit"/>
</form>
    <form action="" method="post">
    <table>
    <?php
    if(isset($_POST['submit'])){
    for($i=1;$i<=$_POST['number'];$i++){
    echo "<tr>
    <td><input type='text' name='firstName$i' /></td>
    <td><input type='text' name='lastName$i' /></td>
    </tr>";
    }
    $i-=1;
    echo "<input type='hidden' name='hide' value='$i' …
Run Code Online (Sandbox Code Playgroud)

php arrays while-loop

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

如何在while循环中填充数组并在每次迭代时获取新范围?

问题是我只得到表中的最后一个值.我认为它是因为我在构建数组时将其值引用到同一个对象,并且它不断变化.我知道while循环不会为每个迭代创建一个新的范围,这是问题所在.

为每次迭代获取新范围的最佳方法是什么?

码:

    $namesArray= array();
        while ($row=mysql_fetch_array($result))
        {
        $nameAndCode->code = $row['country_code2'];
        $nameAndCode->name = $row['country_name'];           
        array_push($namesArray,$nameAndCode);


        } 
return $namesArray;
Run Code Online (Sandbox Code Playgroud)

php arrays loops while-loop

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

Java,查找数组中的最小数字

这是过去一篇论文中的一个问题.我被要求创建一个静态方法arrayMin来查找数组中的最小值arr.

我必须使用while循环,并且在每次迭代时,变量min将返回第一个i元素中的最小数字.

有没有办法在不调用另一个方法/ for循环和严格使用while循环的情况下执行此操作,因为问题仅值4%(包括编写循环不变量和javadoc).不确定我是否过度复杂化了问题.

public class Revision {

public static int arr[] = new int[] { 5, 8, 4, 3, 6, 2 };
public static int min = 1;

public static int arrayMin() {

    int i = 0;

    if (arr == null) {
        return 0;

    } else {
        while (i < arr.length) {
            // some function/method call to find smallest number of arr[i]
            i++;
            return min;
        }
    }
    return min;
}

public …
Run Code Online (Sandbox Code Playgroud)

java while-loop

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

Python范围问题

我正在编写一个程序,在这个程序中需要确定数字的范围,好像我在user_input'a'中放了15,所以它应该打印范围从'a'到'b'但不幸的是我无法解决它的问题有人请帮帮我.这是我的代码:

a = int(raw_input("How many did you say you're going to count down? "))
b = int(raw_input("When are you actually going to stop? "))
i = 0
for i in range(a, b):
    i = i + 1
    print i 
Run Code Online (Sandbox Code Playgroud)

我希望它像这样工作:

How many did you say you're going to count down? 15
When are you actually going to stop? 8
15
14
13
12
11
10
9
8
Run Code Online (Sandbox Code Playgroud)

要么

How many did you say you're going to count down? 6
When …
Run Code Online (Sandbox Code Playgroud)

python range while-loop

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

在Python中无限循环

无论我做什么,我都无法在循环工作时得到这个; 除非我硬编码.

count = 0
value = raw_input('How many?')
print value
while (count <= value):
        print "a"
        count= count + 1
Run Code Online (Sandbox Code Playgroud)

起初我尝试使用命令行参数,使用sys.argv [1]作为值,但我遇到了同样的问题.这看起来很简单,但我不能为我的生活弄清楚我做错了什么.

python infinite-loop while-loop

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

循环时程序不会进入try块

我正在努力为学校写一个程序.分配是循环并要求用户输入文件.当找不到文件或者用户退出循环时(在我的情况下,他们点击joptionpane上的取消)输出每个文件的数据.到目前为止这是我的代码:

/**
 * @param args
 */
public static void main(String[] args) {

    // Lists to hold file data
    List<String> fileNames = new ArrayList<String>();
    List<Integer> numChars = new ArrayList<Integer>();
    List<Integer> numWords = new ArrayList<Integer>();
    List<Integer> numLines = new ArrayList<Integer>();
    String inFile;

    while ( (inFile = JOptionPane.showInputDialog("Enter a file name:")) != null) {

        // Create new file object instance
        File file = new File(inFile);

        try {
            fileNames.add(inFile);  // File name as a string
            // Pass file objects
            numChars.add( getNumChars(file));
            numWords.add( getNumWords(file));
            numLines.add( …
Run Code Online (Sandbox Code Playgroud)

java loops exception try-catch while-loop

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

单行Readline()!在VB.NET中= null

可能重复:
如何在Visual Basic中使用while循环读取StreamReader?

在C#中,我将循环一个streamreader,如下所示.

while ((line = reader.ReadLine()) != null)
{
    list.Add(line); // Add to list.
    Console.WriteLine(line); // Write to console.
}
Run Code Online (Sandbox Code Playgroud)

什么是VB.NET中的等价物?

我试过了

while ((line = reader.ReadLine()) IsNot Nothing)
Run Code Online (Sandbox Code Playgroud)

但它没有用.

vb.net while-loop

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

JAVA - 使用FOR,WHILE和DO WHILE循环来加1到100

正如标题所述,我无法理解循环,并提出了一种简单的1到100总和的方法,但就像我说的那样,循环让我有些困惑.我想我有FOR循环.

这就是我想出来的.

int sum = 0; 
for (int i = 1; i <= 100; i++) sum += i; 
System.out.println("The sum is " + sum);
Run Code Online (Sandbox Code Playgroud)

java for-loop while-loop do-while

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

python重复程序,而真实

我试图让我的程序在用户输入y/n时重复,但是我对如何使用这种类型的输入时使用true感到困惑,下面是一些代码.

again = input("Would you like to play again? enter y/n:  ")
if again == "n":
    print ("Thanks for Playing!")
    quit

if again == "y":
    print ("Lets play again..")
    ????
Run Code Online (Sandbox Code Playgroud)

另外,如果用户输入不同的字符,我想做一个else语句,但我不确定如何去做,因为我有2个不同的if语句.

python if-statement while-loop

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

如何在Coldfusion中创建随机字符串时检查重复值?

我正在运行Coldfusion8/MySQL 5.0.88并拥有带有id的表,这是随机字符串az 0-9

我想在表中创建新记录时避免重复条目,但我不确定如何正确设置它.

这是我有的:

<cfset variables.listOfAppIds = "">

<!--- get all ids --->
<cfquery datasource="db" name="app_ids">
    SELECT app_id FROM apps
</cfquery>

<!--- create comma-separated list --->
<cfloop query="app_ids">
    <cfset variables.listOfAppIds = variables.listOfAppIds & "," & app_ids.app_id>
</cfloop>

<!--- create random string and test if it is in listOfAppIds --->
<cfloop condition="#ListFindNoCase(variables.listOfAppIds, variables.rndString, ',')#">
    <cfset variables.stringLength = 10>
    <cfset variables.stringList = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z">
    <cfset variables.rndString = "">
    <cfloop from="1" to="#variables.stringLength#" index="i">
        <cfset variables.rndNum = RandRange(1, listlen(variables.stringList))>
        <cfset variables.rndString = …
Run Code Online (Sandbox Code Playgroud)

random coldfusion loops duplicates while-loop

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