我刚开始学习Ruby,今天遇到了一个问题.
numResults = /\d+/.match(ie.div(:id, 'results_label').text)
puts "Results found: "+numResults.to_s
while(numResults > 0)
.
. some more code
.
Run Code Online (Sandbox Code Playgroud)
我的输出中出现此错误:
Run Code Online (Sandbox Code Playgroud)Exception: undefined method `>' for #<MatchData:0x424c6d4>
这真的很奇怪,因为我在IRB中做了一个while循环,它工作得很好.我不能让循环中的代码执行,因为程序坚持条件.
谁知道什么是错的?
好的,我的语法可能没有代码就可以描述.基本上它应该很容易......但绝不是.
我连续有2个循环......基本上是一样的.我从我的数据库中选择*每个变量,然后我需要基于两个sep构建一个2层的javascript.变量.
所以
我有:
while ($row = mysql_fetch_array($myVariable)) {
// do events
}
Run Code Online (Sandbox Code Playgroud)
然后呢
while ($row2 = mysql_fetch_array($myVariable)) {
// do events
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因它在第二个完全返回NOTHING ...是否有一些我需要重置我的阵列,它可能结束然后我不能只是重新启动.这是一个基本问题,但我无法弄清楚我的生活还有什么不对.那就是问题,这是一个普遍的问题,就是连续两次这样做,并且期望PHP每次都在$ myVariable的开头重新开始,它保存了我的SELECT语句?
谢谢......将全天检查并根据需要进行更新.Stack Overflow的第一个计时器我很乐意看到帮助.
我在我的Java程序的main方法中运行了一个while循环.循环应该运行,直到在程序的keyPressed方法中将布尔标志变量设置为true(我将程序作为KeyListener添加到JFrame).
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class ThreadWhile implements KeyListener {
private boolean flag = false;
public static void main(String[] args) {
//Set up key listening on a dummy JFrame
JFrame frame = new JFrame("Key Detector 9000");
frame.setVisible(true);
ThreadWhile tw = new ThreadWhile();
frame.addKeyListener(tw);
System.out.println("Looping until flag becomes true...");
while(!tw.flag) {
//Commenting the println out makes the loop run forever!
System.out.println(tw.flag);
}
System.out.println("Flag is true, so loop terminated.");
System.exit(0);
}
public void keyPressed(KeyEvent e) {
flag = true; …Run Code Online (Sandbox Code Playgroud) 我最近开始使用PDO,早些时候我只使用mysql ..现在我试图从数据库中获取所有数据.
$getUsers = $DBH->prepare("SELECT * FROM users ORDER BY id ASC");
$getUsers->fetchAll();
if(count($getUsers) > 0){
while($user = $getUsers->fetch()){
echo $user['username']."<br/>";
}
}else{
error('No users.');
}
Run Code Online (Sandbox Code Playgroud)
但它没有显示任何用户,只是一个空白页面..
我正在尝试在while循环中计时while循环,执行所需的总时间,并记录每次循环所需的时间.如果可能的话,我需要一种方法来使用我的代码实现这一点,或者开放我可能还不知道的不同概念.
import random
import time
import sys
def main():
looperCPU = 500
start = time.time()
while (looperCPU != 0):
#start = time.time()
#this is the computation for 3 secs
time.sleep(3)
random_number = random.randint(0,1000)
#Send to printer for processing
#.75 secs to 4.75 secs to generate a pause before printing
secondsPause = random.uniform(.75,4.75)
#This is the printer function
printerLooper = True
while printerLooper == True :
print("Sleeping for ", secondsPause, " seconds")
print(random_number)
printerLooper = False
#
print("total time taken this …Run Code Online (Sandbox Code Playgroud) 这似乎应该是一个简单的问题,但我找不到一个好的答案.有没有办法在foreach循环上设置条件?我想要这样的事情:
foreach ($array as $value WHILE $condition == true)
{ //do some code }
Run Code Online (Sandbox Code Playgroud)
当然我可以在foreach循环中放置一个if条件,如下所示:
foreach ($array as $value)
{
if($condition == true)
{//do some code}
}
Run Code Online (Sandbox Code Playgroud)
唯一的一点是,我想在if条件变为false时停止迭代数组,以提高性能.不需要遍历foreach循环的其余部分来确定$ condition在变为false时为false.
有什么建议?我错了一些明显的东西吗?
问题
找到1000以下可以被3或5整除的所有数字的总和
我创建的一个解决方案
x <- c(1:999)
values <- x[x %% 3 == 0 | x %% 5 == 0]
sum(values
Run Code Online (Sandbox Code Playgroud)
第二种解决方案我无法开展工作并需要帮助.我把它贴在下面. 我正在尝试使用循环(在这里,我使用while(),然后我会尝试()).我仍然在努力保持对索引(向量中的位置)的引用与向量内的值/观察分开.循环似乎让我更难以区分这两者.
为什么这不能产生欧拉#1的答案?
x <- 0
i <- 1
while (i < 100) {
if (i %% 3 == 0 | i %% 5 == 0) {
x[i] <- c(x, i)
}
i <- i + 1
}
sum(x)
Run Code Online (Sandbox Code Playgroud)
换句话说,这是我理解的事情:
我猜x [i] < - c(x,i)不是向元素x添加元素的正确方法.我该如何解决这个问题以及其他什么不准确?
我正在使用此方法缩小TextView文本,因为它的名字暗示:
public static float shrinkTextToFit(String caller, float availableWidth, TextView textView, float startingTextSize, float minimumTextSize) {
startingTextSize = textView.getTextSize() < startingTextSize ? textView.getTextSize() : startingTextSize;
Log.i("123", "=========================");
Log.i("123", caller + " called shrinkTextToFit");
CharSequence text = textView.getText();
float textSize = startingTextSize;
textView.setTextSize(startingTextSize);
while (!TextUtils.equals(text, (TextUtils.ellipsize(text, textView.getPaint(), availableWidth, TextUtils.TruncateAt.END)))) {
textSize -= 2;
Log.i("123", "textSize: " + textSize);
if ((textSize <= minimumTextSize) || (textSize <= 0)) {
break;
} else {
textView.setTextSize(textSize);
}
}
return textSize;
}
Run Code Online (Sandbox Code Playgroud)
而且我只有这个设备有堆栈溢出(有时候不会发生):
我正在进行一项练习,其中基于for循环的一小段代码被转换为使用while循环执行相同的操作.目的转换是错误的,如下所示:
int sum = 0;
for (int i = 0; i < 4; i++) {
if (i % 3 == 0) continue;
sum += i;
}
System.out.println(sum); // prints 3
Run Code Online (Sandbox Code Playgroud)
这转换为:
int i = 0, sum = 0;
while (i < 4) {
if (i % 3 == 0) continue;
sum += i;
i++;
}
System.out.println(sum); // will not print
Run Code Online (Sandbox Code Playgroud)
在练习中,我被要求解释为什么转换是错误的然后修复它.我的想法是:
随着初始值I = 0,这将触发继续瞬间进入将循环后,因为(0%3 == 0)将使if语句真.只要初始值为0,这将执行循环,以便跳过它无数次.我尝试更改i = 1的初始值,但是打印了noe sum.然后我尝试在执行if语句 …
我正在做作业。而且我找不到如何执行此解决方案。
我已经尝试过在声明中使用中断,但没有任何回报。问题是“完成以下程序,以使循环在找到大于1000且能被33和273整除的最小正整数时停止。”
这是我尝试执行的代码
n = 1001 #This one is required
while True: #This one too
for i in range(n,___): # I don't know what should i put in the blank
if i%33 == 0 and i%273 == 0: # I really confused about this line
break # Should i break it now?, or in the other lines?
print(f"The value of n is {n}") #This one is also required
Run Code Online (Sandbox Code Playgroud)
我不知道我应该在哪些行中打断(或者我不必使用它?),或者我应该创建一个调用列表最小数量的函数?
我为自己的语言以及接受每条评论而对自己的编程技能有多愚蠢感到抱歉。谢谢