我想制作一个像这样的数组
$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) 问题是我只得到表中的最后一个值.我认为它是因为我在构建数组时将其值引用到同一个对象,并且它不断变化.我知道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) 这是过去一篇论文中的一个问题.我被要求创建一个静态方法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) 我正在编写一个程序,在这个程序中需要确定数字的范围,好像我在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) 无论我做什么,我都无法在循环工作时得到这个; 除非我硬编码.
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]作为值,但我遇到了同样的问题.这看起来很简单,但我不能为我的生活弄清楚我做错了什么.
我正在努力为学校写一个程序.分配是循环并要求用户输入文件.当找不到文件或者用户退出循环时(在我的情况下,他们点击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) 在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)
但它没有用.
正如标题所述,我无法理解循环,并提出了一种简单的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) 我试图让我的程序在用户输入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语句.
我正在运行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) while-loop ×10
java ×3
loops ×3
python ×3
arrays ×2
php ×2
coldfusion ×1
do-while ×1
duplicates ×1
exception ×1
for-loop ×1
if-statement ×1
random ×1
range ×1
try-catch ×1
vb.net ×1