while-loop

Android postDelayed works but can't put it in a loop?

情到浓时终转凉″ 提交于 2019-12-04 18:17:55
Sorry I am a noob I've read countless tutorials about making a simple timer and was wondering why it doesn't work until I noticed it is the while-loop causing the issue o.O I have removed it and then it works but only 1 time I need to use the loop though so the movement finishes :C Heres the code: old_x is the coordinate from an ImageView and new_x from the onTouch Event, maybe the problem because I am casting them as an int? I don't know what I need to do so it works please help O: while(old_x != new_x) { timedMoveIV(100); old_x = (int)img.getX(); } It calls this method which works if I do it

Java loop efficiency

元气小坏坏 提交于 2019-12-04 17:42:12
问题 I'm comparing the efficiency of nested for, while and do-while loops in Java, and I've come across some strange results that I need help understanding. public class Loops { public static void main(String[] args) { int L = 100000; // number of iterations per loop // for loop double start = System.currentTimeMillis(); long s1 = 0; for (int i=0; i < L; i++) { for (int j = 0; j < L; j++) { s1 += 1; } } double end = System.currentTimeMillis(); String result1 = String.format("for loop: %.5f", (end

The difference between while and do while C++? [duplicate]

守給你的承諾、 提交于 2019-12-04 17:10:49
This question already has answers here : 'do…while' vs. 'while' (27 answers) Closed 5 years ago . I would like someone to explain the difference between a while and a do while in C++ I just started learning C++ and with this code I seem to get the same output: int number =0; while (number<10) { cout << number << endl; number++ } and this code: int number=0; do { cout << number << endl; number++ } while (number<10); The output is both the same in these both calculations. So there seem to be no difference. I tried to look for other examples but they looked way to difficult to understand since it

Tkinter: Updating Labels mid-loop

喜夏-厌秋 提交于 2019-12-04 16:57:49
I would like to update a Label in my GUI to be a sort of progress bar, display how complete a data transfer is. Everywhere I look, people say to use the textvariable option of Label and then to set the string and the label updates. This does not work for me. The label updates at the end of the data collection loop. I don't know too much about programming in depth but I imagine that Python is not refreshing Tkinter until after it is finished with the data collection loop rather than mid loop. Here's the data collection loop: def getdata(self, filename): data=[] count=0 percentage=0 self.ser

Linux bash Timer

淺唱寂寞╮ 提交于 2019-12-04 16:56:05
Ok, stupid newbie question here. I thought I was making a countdown timer. This is supposed to count down from 5 and once it is at 0 then execute the echo "time is up clown" then end. What am I doing wrong here? seconds=5 date1=$((`date +%s` + $seconds)); while [ "$date1" -ne `date +%s` ]; do if (!$date1 -lt ((`date +%s` + $seconds)+1)); then echo "time is up clown"; break; fi; echo -ne "$(date -u --date @$(($date1 - `date +%s` )) +%H:%M:%S)\r"; done #!/bin/bash SECS=5 while [[ 0 -ne $SECS ]]; do echo "$SECS.." sleep 1 SECS=$[$SECS-1] done echo "Time is up, clown." 来源: https://stackoverflow

for or while loop to do something n times

送分小仙女□ 提交于 2019-12-04 16:16:14
问题 In Python you have two fine ways to repeat some action more than once. One of them is while loop and the other - for loop. So let's have a look on two simple pieces of code: for i in range(n): do_sth() And the other: i = 0 while i < n: do_sth() i += 1 My question is which of them is better. Of course, the first one, which is very common in documentation examples and various pieces of code you could find around the Internet, is much more elegant and shorter, but on the other hand it creates a

Does while loop have two Arguments?

China☆狼群 提交于 2019-12-04 16:04:26
My ma'am gave me one question to solve. To predict the output of the following code. #include <stdio.h> int main() { int i = 0, j = 0; printf("Output is : "); while (i < 5, j < 10) // Doubt: how does while accept 2 arguments?? and how it works?? { i++; j++; } printf("%d, %d\n", i, j); } I thought it was a syntax error. But when I tried to run, it gave me output. Output is : 10, 10 But How? Can anyone explain? But if I remove the first printf statement printf("Output is : "); and run it, my antivirus give me a alert that a Trojan is detected. But how it becomes a Trojan ? The comma operator is

Perl, A hash of arrays: adding and removing keys, adding to an array, all in a while loop

老子叫甜甜 提交于 2019-12-04 15:30:58
I have a hash which should contain certain keys which are linked to their own arrays. To be more specific, the hash keys are quality values and the arrays are sequence names. If there already is an array for that quality, I'd want to add the sequence name to the array that is linked to the quality in question. If there isn't one, I want to create one and add the sequence name to it. All this is done in a while loop, going through all the sequences one by one. I've tried to do things like in Perl How do I retrieve an array from a hash of arrays? but I can't seem to get it right. I just get

How to run a while loop as long as there is no error [duplicate]

独自空忆成欢 提交于 2019-12-04 15:08:17
This question already has answers here : Asking the user for input until they give a valid response (18 answers) Closed 4 years ago . I am running a code that might get an error, but I want to run it as long as there is no error. I thought about something like that: while ValueError: try: x = int(input("Write a number: ")) except ValueError: x = int(input("You must write a number: "))` You were quite near while True: try: x = int(input("Write a number: ")) break except ValueError: print("You must write a number: ") To know more about exception handling, refer the documentation As an addition

How to do a while loop with a string redirected into it

有些话、适合烂在心里 提交于 2019-12-04 14:32:23
问题 Im trying to loop though a string with HTTP links inside and newlines, I want to loop over a line at a time. At the moment I have echo -e "$HTTP_LINKS" | while read HTTP_S_LINK ; do TEST_STRING="test" done But this way I don't have access to the TEST_STRING out side the loop, which is what I want. I'm using the while loop so that it will loop though each newline in $HTTP_LINKS and not just the words in the string. (I don't want to use a for loop with IFS set to \n) I thought maybe I could