loops

When the input will stop?

自古美人都是妖i 提交于 2021-01-28 06:58:00
问题 Hi guys i have the following question: string s; std::cout << "give me a string" << std::endl; while (std::cin >> s){ std::cout << "give me a string" << std::endl; } cin.clear(); if s was an integer then i know that if i typed a char then the while would break. If it was an integer and typed a double the same would happened. But what about strings ?? Somewhere I read that in windows (as I am writting at netbeans in windows) you have to press ctrl+z and then it stops as it is taken as a false

addEventListener works in simple for loop but doesn't work with for-in loop

一笑奈何 提交于 2021-01-28 05:40:38
问题 When I use simple for loop, addEventListener works well in for loop. But when I use for-in loop, it makes error like Uncaught TypeError: checklist[i].addEventListener is not a function This is my work-well code. var checklist = document.querySelectorAll(".checklist"); for (var i = 0, len = checklist.length; i < len; i += 1) { checklist[i].addEventListener('change', function (event) { alert('test'); }); } This is my Error code. var checklist = document.querySelectorAll(".checklist"); for (var

Looping over the last few entries of an array

做~自己de王妃 提交于 2021-01-28 05:38:18
问题 I'm trying to have a forEach loop over an array, but only the last few entries. I'd know how to do this in a for loop, that'd look a bit like this: var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; /* This will loop over the last 3 entries */ for(var x = arr.length; x >= 7; x--){ console.log(arr[x]); } Would there be any way of achieving the same results in a forEach loop? 回答1: You can use slice() and reverse() methods and then forEach() loop on that new array. var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9,

Java programming - nested for loops for minesweeper game

允我心安 提交于 2021-01-28 05:28:44
问题 Currently making a minesweeper game and stuck on being able to loop through the 8 surrounding cells to find the number of surrounding bombs. I have a helper method that I believe works fine that is used to check if the cell (passed by numberOfAdjacentBombs) contains a bomb. How do I go about creating a nested for loop that will loop through each surrounding cell? e.g (row-1, col-1), (row-1, col), (row-1, col+1). Any help or hints is appreciated, thanks! :-) private int numberOfAdjacentBombs

Image does not remain on screen after executing for loop in pygame

北战南征 提交于 2021-01-28 05:26:49
问题 I have been trying to modify the code from this Tutorial so that after a bullet strikes an enemy the player.png image is shown at position x = 60 and y = 48 . But the image does not remain fixed , it just appears and disappears. I don't know exactly where the wrong or missing element is in the code but I suspect that one of the causes is some misuse of my part of the for loop within the draw_reaction function. My player.png image My MWE code: import pygame import random # Define some colors

Filter out duplicate values in array in C++

前提是你 提交于 2021-01-28 05:20:57
问题 I have a row of ten numbers for example: 5 5 6 7 5 9 4 2 2 7 Now I want a program that finds all duplicates and gives them out in the console like 3 times 5, 2 times 2, 2 times 7. While I did code an algorithm that finds duplicates in a row of numbers I can't give them out in the console as described. My program will output: 3 times 5 2 times 5 2 times 7 2 times 2 How can I solve this problem? #include <iostream> using namespace std; int main() { int arr[10]; int i,j; int z = 1; for(i = 0; i

Is there a way to loop through data based on factor in a column and add up the number of rows?

▼魔方 西西 提交于 2021-01-28 05:08:42
问题 I have some data in which I have multiple observations of the same event. Based on a threshold of time, I want to condense the observations. But I want to know how many I am condensing (i.e. how many observations become one). I'm not sure how to loop through my dataframe in such a way to do that. I've tried writing a for loop, if statements, while statements, and have searched tirelessly on google and on stack overflow. Nothing seems to relate to what I need to do. here is a subset of my data

Using tryCatch() to catch a bootstrap loop

强颜欢笑 提交于 2021-01-28 05:03:49
问题 I have insufficient knolwedge on the use of tryCatch() but have found it hard to find a good solution in the ongoing discussions. I have a self-programmed function which returns an object. It is actually a list object, but for simplicity assume it is a scalar. I am using a for() loop to bootstrap this function. My loop is of the following form: boot<-1000 for(i in 1:boot){ bootstrap_data<-data[sample(nrow(data),nrow(data),replace=T),] out[i]<-myfunction(bootstrap_data,X,...) } myfunction()

How can I print every minute using Datetime with Python

荒凉一梦 提交于 2021-01-28 05:02:05
问题 As an example, I want to print, "1 min", every time 1 minute has passed using time or datetime. I cant use time.sleep(60) because I have more code that needs to run in the whileloop every update. I need a way to check if datetime.now() is greater than 1 minute ago. Thanks! import time import datetime as dt t = dt.datetime.now() while True: if 60 seconds has passed: print("1 Min") 回答1: This may be what you are looking for: import datetime as dt from time import sleep t = dt.datetime.now()

How can I web scraping without the problem of null website in R?

梦想的初衷 提交于 2021-01-28 04:13:44
问题 I need to extract information about species and I write the following code. However, I have a problem with some absent species. How is it possible to avoid this problem. Q<-c("rvest","stringr","tidyverse","jsonlite") lapply(Q,require,character.only=TRUE) #This part was obtained by pagination that I not provided to have a short code sp1<-as.matrix(c("https://www.gulfbase.org/species/Acanthilia-intermedia", "https://www.gulfbase.org/species/Achelous-floridanus", "https://www.gulfbase.org