while-loop

Fibonacci function not accepting 0 and not displaying the last term only

陌路散爱 提交于 2019-12-11 13:10:07
问题 I wrote a function that displays the Fibonacci sequence up to the nth term. The code runs fine, but I want to make two changes and am not sure how to do so. Here is my code: function [ F ] = get_fib( k ) F(1) = 1; F(2) = 1; i = 3; while k >= i; F(i) = F(i-1) + F(i-2); i = i + 1; end end The first problem is that the code does not accept 0 as an input. I tried changing the function to: function [ F ] = get_fib( k ) F(0) = 0; F(1) = 1; F(2) = 1; i = 3; while k >= i; F(i) = F(i-1) + F(i-2); i =

golfscript nested while loops

家住魔仙堡 提交于 2019-12-11 12:50:05
问题 Are nested while loops broken in golfscript or do I not know how to use them? I want to iterate Q from 5 to 0, and for each iteration, iterate Z from 10 to 0. The single loops work well separately, and they seem self-contained (not relying on the stack between operations): 5:Q; {"Q:"Q+ p Q} { Q 1- :Q; }while 10:Z;{"Z:"Z+ p Z}{Z 1- :Z;}while Output: "Q:5" "Q:4" "Q:3" "Q:2" "Q:1" "Q:0" "Z:10" "Z:9" "Z:8" "Z:7" "Z:6" "Z:5" "Z:4" "Z:3" "Z:2" "Z:1" "Z:0" But if I put the Z loop inside the Q loop,

java nested while loop in result set

醉酒当歌 提交于 2019-12-11 12:46:39
问题 I want to merge results from 2 resultSet which are results returned from sql queries. What is the best way to accomplish this? My plan is lopping over 2 result sets to print out the final result from both result sets. Both results set have the same id column which are their first column. But for the inner while loop, I only get the first value printed out. I thought when there is a match, it prints the value from the second result set, and break out of the loop. Then when there is another

While loop to check user input for only positive integers

跟風遠走 提交于 2019-12-11 11:57:36
问题 I need help implementing a loop that keeps telling the user to enter only positive integers, but I don't know where to start. Can someone help me. import java.util.Scanner; public class GCD { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a positive integer: "); int firstN = input.nextInt(); System.out.print("Enter another positive integer: "); int secondN = input.nextInt(); System.out.println("The GCD of " + firstN + " and " + secondN

PHP loop listen for user input

旧巷老猫 提交于 2019-12-11 11:56:03
问题 I have a PHP script that run on console. while(1) { doStuff(); sleep(2); } I need to accept input from the console. I don't want loop stop each time and wait for me to input some text. What i want is while loop continue as normal, if i type something in console, php script able to read that text and update some variable. Can this be done ? 回答1: You can do this with non-blocking I/O. You'll need the stream_set_blocking method and stream_select: stream_set_blocking(STDIN, FALSE); while (1) {

how to make a sprite bounce of the edges of the window in pygame

别说谁变了你拦得住时间么 提交于 2019-12-11 11:46:13
问题 So far I have my program bouncing from left to right, but now I want to have in bounce of the walls. How can I get to that? delta = 5 u = 1 # x coordinate u += delta if u >= 440: #480 is the window size, I have it stopping at 440 delta = -5 #Go back by 5 elif u < 0: delta = 5 #Go up by 5 Bigger Snip it of code, It has the solution but the sprite goes off the screen, To Run the code make sure you have a picture called "bird.png" or change the picture: import pygame import random class mySprite

C - How to read inputs

本秂侑毒 提交于 2019-12-11 11:39:40
问题 it's part of the homework, but it's not the main part I already made the main function by manually changing the data by myself, but I really don't get how to read the user inputs correctly. Here are what the inputs look like: 3 1 2 9 6 1 1 3 4 4 6 0 So basically the first input is the size of the # of the next inputs. So for 3, the array is size 3 with [1,2,9] being the elements, and for 6, the array size is 6 with [1,1,3,4,4,6] being the elements And when the input is 0, the program

Read variables from two files and output in a pattern on 3rd file

泪湿孤枕 提交于 2019-12-11 11:33:25
问题 I have 2 files like below: file1: a1,b1,c1,d1,e1,f1,g1,h1 a2,b2,c2,d2,e2,f2,g2,h2 a3,b3,c3,d3,e3,f3,g3,h3 a4,b4,c4,d4,e4,f4,g4,h4 file2: x1,y1,z1 x2,y2,z2 x3,y3,z3 x4,y4,z4 I want to read simultaneously from both and output the variables in a pattern like below: a1,b1,c1,d1,x1,e1,f1,y1,g1,z1,h1 a2,b2,c2,d2,x2,e2,f2,y2,g2,z2,h2 a3,b3,c3,d3,x3,e3,f3,y3,g3,z3,h3 a4,b4,c4,d4,x4,e4,f4,y4,g4,z4,h4 Good news - I've managed to achieve it !! Bad news - Too many arrays and while loops (too much

How do I use while loops to create a multiplication table in python?

≯℡__Kan透↙ 提交于 2019-12-11 11:10:18
问题 This is my code, it outputs a multiplication table but it's not what I wanted! num = int(input("Multiplication using value? : ")) while num <= 10: i = 1 while i <= num: product = num*i print(num, " * ", i, " = ", product, "\n") i = i + 1 print("\n") num = num + 1 I am basically creating a multiplication table from the user's input from 1-9. Ex. If the user inputs "3" I should get this output: 1*1=1 1*2=2 1*3=3 2*1=2 2*2=4 2*3=6 3*1=3 3*2=6 3*3=9 This is my first time learning Python, I could

I want to check for both negative number and non-number

戏子无情 提交于 2019-12-11 10:54:25
问题 I have code to check for non numbers but also wish to include a check for negative numbers. If the number is negative or not a number, they have to re-enter info. I tried putting an if(depValue < 0).... after try{ and before catch but that didn't work. It doesn't make sense to me if I were to put the if statement after the while loop. String depIn = ""; BufferedReader depositInput = new BufferedReader(new InputStreamReader(System.in)); while(true){ System.out.print("Amount to deposit: ");