for-loop

Why does a semicolon after a for statement cause a compile error?

北慕城南 提交于 2020-01-16 00:25:32
问题 For my Java class, we are asked to add a semicolon to a working for statement and explain why the output is what it is. I don't understand why adding the semicolon creates an erroneous tree type error resulting in the code being unable to compile. Below the code is the output; I have also added backslashes to the any tag because it wasn't displaying otherwise. So, why does a semicolon after a for statement cause such an error? Thanks in advance. package fordemo; import java.util.Scanner;

prime number python for loops

人走茶凉 提交于 2020-01-15 14:27:12
问题 Question: A program that take a positive integer n as input and returns True if n is a prime number, otherwise returns False. My Answer: n = int(input("Enter a number: ")) for i in range(2,n): if n%i == 0: print(False) print(True) when I enter a prime number it works but when I enter a non prime number it doesn't work. Example: >>> Enter a number: 12 False False False False True >>> please help! 回答1: You can break and use else : n = int(input("Enter a number: ")) for i in range(2, n): if n %

Lua for loop does not iterate properly

陌路散爱 提交于 2020-01-15 12:33:23
问题 I am in dire need of help with a for loop. I'm trying to go through a for loop in Lua with the Corona SDK, but I am doing something wrong but I don't know what. See below for my code: function moveLift(event) for j=1,4,1 do if event.phase == "began" then markY = event.target.y elseif event.phase == "moved" then local y = (event.y - event.yStart) + markY event.target.y = y elseif event.phase == "ended" then if (hasCollided( event.target, hotSpots[j] )) then print("hasCollided with floor: ",

Scrape and Loop with Rvest

蹲街弑〆低调 提交于 2020-01-15 10:59:29
问题 I have reviewed several answers to similar questions on SO related to this similar topic but neither seem to work for me. (loop across multiple urls in r with rvest) (Harvest (rvest) multiple HTML pages from a list of urls) I have a list of URLs and I wish to grab the table from each and append it to a master dataframe. ## get all urls into one list page<- (0:2) urls <- list() for (i in 1:length(page)) { url<- paste0("https://www.mlssoccer.com/stats/season?page=",page[i]) urls[[i]] <- url } #

based on dataframe column result all following rows equal a repetitive value until result changes and new repetitive value occurs

故事扮演 提交于 2020-01-15 10:25:06
问题 My simplified dataframe is as follows: df = pd.DataFrame() df['A'] = ('IGNORE','IGNORE','IGNORE','YES','IGNORE','YES','YES','YES','IGNORE','IGNORE','IGNORE','YES','IGNORE','IGNORE','IGNORE','IGNORE','IGNORE','IGNORE','IGNORE','IGNORE','IGNORE', 'NO','IGNORE','IGNORE','IGNORE','IGNORE') I need to reverse dataframe (which I know I can do via df = df[::-1]) then make column B as follows. if 'YES' occurs then following rows result in 'GOOD' until a 'YES' or 'NO' occurs again and via versa for 'NO

JavaScript for loop increment behaves strangely [duplicate]

烂漫一生 提交于 2020-01-15 08:16:01
问题 This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Closed 4 years ago . I have the following JS: for ( var i = 1; i <= 2; i++ ) { $(window).load(function() { alert(i); }); } When the page loads, it gives me an alert twice as expected. But the strange thing is the value of i is 3 on both alert. I expect the value of i is 1 on the first alert and 2 on the second alert. What causes the problem? Thank you in advance! UPDATE #1 What if

JavaScript for loop increment behaves strangely [duplicate]

时间秒杀一切 提交于 2020-01-15 08:15:32
问题 This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Closed 4 years ago . I have the following JS: for ( var i = 1; i <= 2; i++ ) { $(window).load(function() { alert(i); }); } When the page loads, it gives me an alert twice as expected. But the strange thing is the value of i is 3 on both alert. I expect the value of i is 1 on the first alert and 2 on the second alert. What causes the problem? Thank you in advance! UPDATE #1 What if

How do I use the indices of nested for-loops to generate a consecutive list of numbers?

五迷三道 提交于 2020-01-15 07:37:27
问题 E.g.: for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { for (int k = 0; k < 5; k++) { ... } } } That's 3 * 4 * 5 = 60 times executing the innerst code. Now I want to use the values of the indices i, j, and k to generate all numbers from 0 to 59 (not necessarily sorted). 回答1: Ok, got it. Make k count 1, j count k's limit, i count j's limit times k's limit. for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { for (int k = 0; k < 5; k++) { int c = k * 1 + j * (5) + i * (5 * 4);

Compute sum of series

怎甘沉沦 提交于 2020-01-15 05:56:07
问题 I need to compute the sum of this series I need the output this way: If n = 3; x = function_name(n) I need to get x = 11. If n = 5; x = function_name(n) I need to get x = 45 . I believe I need a for-loop to iterate; but am finding it difficult to iterate the increment value itself. 回答1: inc=2; sum=1; next=1; n=input('what is n?\n'); for i=2:n next=next+inc; sum=sum+next; inc=inc+2; end disp('sum is '); disp(sum); 回答2: I guess you want the sum of the cumsum of the differences d of the numbers:

Google Spreadsheet - Convert multiple columns to one column

穿精又带淫゛_ 提交于 2020-01-15 05:18:06
问题 I want to loop through a set of rows in a Google Spreadsheet that look like this: XXX 123 234 234 YYY 789 098 765 ZZZ 76 123 345 End Result Needs to Be: XXX: 123 XXX: 234 XXX: 234 YYY: 789 YYY: 098 etc. My current code: function loopshoplocations(){ var sheet = SpreadsheetApp.getActiveSheet(); var data = sheet.getRange('A4:A8').getValues(); var i=0; for(i = 0; i < 4; i++){ return ('Shop Location: ' + data[i][0]); }} 回答1: This code assumes that there is a header row on row one. The data gets