我一直在尝试一些涉及循环的基本练习.
有人能告诉我为什么以下片段有不同的输出?
while (i<3)
{
while(j<3)
{
printf("(%d %d) ",i,j);
j++;
}
i++;
}
Run Code Online (Sandbox Code Playgroud)
产量
(0 0) (0 1) (0 2)
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("(%d %d) ",i,j);
}
Run Code Online (Sandbox Code Playgroud)
产量
(0 0) (0 1) (0 2) (1 0) (1 1) (1 2) (2 0) (2 1) (2 2)
他们不应该有相同的输出吗?
所以我有一个基于两个查询嵌套在另一个循环中的循环.我有第一个循环工作正常 -
$sql_categories = mysql_query("SELECT * FROM $categories_table");
$results = mysql_query("SELECT * FROM $events_table");
while ($num_rows = mysql_fetch_assoc($sql_categories)) {
extract($num_rows);
echo "<h2>$category_name</h2>";
// Begin second loop to output events
while/for(not sure) {
}
}
Run Code Online (Sandbox Code Playgroud)
我想输出所有$vars相应的第二个循环$category_id.在第二个查询中,匹配值为$event_category_id.
我不知道这是否有意义,但我想要得到的基本上是 -
<h2>Category One</h2>
Event Name
Event Name
Event Name
<h2>Category Two</h2>
Event Name
Event Name
Event Name
Run Code Online (Sandbox Code Playgroud)
其中"事件名称"对应于"类别名称"
我正在使用的两张桌子看起来像这样 -
CREATE TABLE `wp_wild_dbem_categories` (
`category_id` int(11) NOT NULL auto_increment,
`category_name` tinytext NOT NULL,
PRIMARY KEY (`category_id`)
) ENGINE=MyISAM …Run Code Online (Sandbox Code Playgroud) 我有一个文件:
P 0.5 0.6 0.3
30 300
80 150
160 400
200 150
250 300
T
r 45 0 0
s 0.5 1.5 0 0
t 200 –150
.
.
.
Run Code Online (Sandbox Code Playgroud)
当我读到'P'时,我知道将会有3个花车.接下来是有限数量的X和Y坐标.数字会有所变化,直到达到"T",我必须承认.然后可能会有一个'r','s'或't'后跟一些值.
无论如何我知道如何识别'P'然后接收2个浮点数然后我知道我必须有一个while循环的X和Y坐标,当我到达'T'时它将停止.我不太了解C++使循环停止并识别'T'然后做其他事情.
一个例子来解释将不胜感激.提前致谢!
感谢您查看此问题.
我有一个完全有效页面的页面,并且有一个PHP循环<li>为表的每个条目引入一个.
当我在本地查看此页面时,它看起来100%正常,但是当在线浏览页面时,左侧栏(创建此标记会随机混合使用<div>,<li>并且我不知道问题是什么.
这个问题是在FF mac和PC上(safari看起来不错)
见页 (问题在左侧)
<?php do { ?>
<li class="clear-block" id="<?php echo $row_Recordset1['penSKU']; ?>">
<a title="Click to view the <?php echo $row_Recordset1['penName']; ?> collection" rel="<?php echo $row_Recordset1['penSKU']; ?>">
<img src="prodImages/small/<?php echo $row_Recordset1['penSKU']; ?>.png" alt="" />
<div class="prodInfoCntnr">
<div class="basicInfo">
<span class="prodName"><?php echo $row_Recordset1['penName']; ?></span>
<span class="prodSku"><?php echo $row_Recordset1['penSKU']; ?></span>
</div>
<div class="secondaryInfo">
<span>As low as .<?php echo $row_Recordset1['price25000']; ?>¢ <!--<em>(R)</em>--></span>
<div class="colorPlacholder" rel="<?php echo $row_Recordset1['penColors']; ?>"></div>
</div>
</div>
<div class="additPenInfo"> …Run Code Online (Sandbox Code Playgroud) 我有一些代码执行一段时间(fscanf!= EOF)循环.但是,即使fscanf执行完毕,我仍需要继续运行该代码,直到满足某些条件.我的意思是我想我可以将代码复制/粘贴到while(fscanf)循环之外,并且只使用全局变量,但这看起来很混乱.当然有人之前遇到过这样的事情并且有一个更清洁的解决方案.
我试着在一段时间内有一个块并在Ruby中开始语句,但是我得到了一个语法错误.有没有其他方式来实现它?
这就是我想要完成的
(1..limit).each { |i|
while (true) do |n|
x = n * (i%n)
puts n if n%i != 0
break if x.even? && !x.zero?
n += 1
end
}
Run Code Online (Sandbox Code Playgroud) 我发现自己遇到了一个面试问题,其目标是编写一个排序算法,对一系列未排序的int值进行排序:
int[] unsortedArray = { 9, 6, 3, 1, 5, 8, 4, 2, 7, 0 };
Run Code Online (Sandbox Code Playgroud)
现在我用Google搜索并发现那里有很多排序算法!最后,我可以激励自己深入研究Bubble Sort,因为它看起来很简单.
我阅读了示例代码并找到了一个如下所示的解决方案:
static int[] BubbleSort(ref int[] array)
{
long lastItemLocation = array.Length - 1;
int temp;
bool swapped;
do
{
swapped = false;
for (int itemLocationCounter = 0; itemLocationCounter < lastItemLocation; itemLocationCounter++)
{
if (array[itemLocationCounter] > array[itemLocationCounter + 1])
{
temp = array[itemLocationCounter];
array[itemLocationCounter] = array[itemLocationCounter + 1];
array[itemLocationCounter + 1] = temp;
swapped = true;
} …Run Code Online (Sandbox Code Playgroud) programming-languages while-loop conditional-statements c#-4.0
在Python IDLE Shell中,似乎我不能使用复合条件表达式和while循环.我也在括号内尝试过它.拿这两个例子:
k=0
m=0
while k<10 & m<10:
print k
k +=1
m+=1
Run Code Online (Sandbox Code Playgroud)
这不会评估第二个条件.但如果我写
while k<10:
print k
k+=1
Run Code Online (Sandbox Code Playgroud)
这确实有效.有没有办法用"和"运算符实现第一个代码块.我用Java做过.我是否只需要将"if"语句放在一起以在Python中实现相同的功能?
以下代码给出了一个错误:"expected';' 在'{'token"之前.谁能明白为什么?
do {
r = rand() % numElements;
} while ([questionsShown containsObject:r] && myCount < numElements) {
//code here…
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
-(void)removeFilesWithPathIndices:(NSIndexSet*)indexSet {
NSInteger index = [indexSet firstIndex];
while(index >= 0) {
[self removeFileWithPathIndex:index];
index = [indexSet indexGreaterThanIndex:index];
}
}
Run Code Online (Sandbox Code Playgroud)
哪个应该遍历NSIndexSet.但是,即使index = -1,while循环也不会停止
NSLog(@"%d", index);
Run Code Online (Sandbox Code Playgroud)
有谁能为我解决这个谜团?:)