while-loop

Storing User Input into an Array [closed]

限于喜欢 提交于 2019-12-12 10:22:34
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago . For a university assignment I need to produce a java system for docking ships in a port. The port has 3 docks, each dock contains 10 spaces to dock ships. However the spaces are different sizes, to accommodate different size ships. Small ships can fit in small medium and large

Anagram / partial anagram detection algorithm finds incorrect answers

坚强是说给别人听的谎言 提交于 2019-12-12 10:22:18
问题 I've written the following method to find out whether a long word contains a shorter word, and the order in which I pass the letters appears to effect the outcome. I've noticed that if I feed it absconds and bassy it correctly reports NO , but if I alphabetize the letters and give it abcdnoss and abssy , it gives YES . I'm not too sure why this is – can anyone spot the issue? - (BOOL) does: (NSString* ) longWord contain: (NSString *) shortWord { while([longWord length] > 0 && [shortWord

C# - Parallelizing While Loop with StreamReader causing High CPU

限于喜欢 提交于 2019-12-12 10:02:42
问题 SemaphoreSlim sm = new SemaphoreSlim(10); using (FileStream fileStream = File.OpenRead("...")) using (StreamReader streamReader = new StreamReader(fileStream, Encoding.UTF8, true, 4096)) { String line; while ((line = streamReader.ReadLine()) != null) { sm.Wait(); new Thread(() => { doSomething(line); sm.Release(); }).Start(); } } MessageBox.Show("This should only show once doSomething() has done its LAST line."); So, I have an extremely large file that I want to execute code on every single

while(foo) vs while(foo != NULL)

非 Y 不嫁゛ 提交于 2019-12-12 09:54:16
问题 Can someone explain how while(foo) vs while(foo != NULL) are equivalent? Also: while(!foo) vs while(foo == NULL) I know that ! is not, but that is about all I know. 回答1: Assuming foo is of pointer type, while (foo) and while (foo != NULL) are exactly equivalent. Both are also equivalent to while (foo != 0) . (In my opinion while (foo != NULL) more clearly expresses the intent.) In any context requiring a condition ( if() , while() , a few others), the expression is treated as true if the

A try-catch method in while loop?

我的梦境 提交于 2019-12-12 09:22:38
问题 I have this code, and I want to put the try-catch inside a while loop. The logic would be, "while there is an input error, the program would keep on asking for a correct input". How will I do that? Thanks in advance. public class Random1 { public static void main(String[] args) { int g; Scanner input = new Scanner(System.in); Random r = new Random(); int a = r.nextInt(10) + 1; try { System.out.print("Enter your guess: "); g = input.nextInt(); if (g == a) { System.out.println("**************")

Linux bash Timer

本秂侑毒 提交于 2019-12-12 09:09:49
问题 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 回答1: #!/bin/bash SECS=5 while [[ 0 -ne

for-each vs for vs while

天涯浪子 提交于 2019-12-12 08:49:54
问题 I wonder what is the best way to implement a "for-each" loop over an ArrayList or every kind of List. Which of the followings implementations is the best and why? Or is there a best way? Thank you for your help. List values = new ArrayList(); values.add("one"); values.add("two"); values.add("three"); ... //#0 for(String value : values) { ... } //#1 for(int i = 0; i < values.size(); i++) { String value = values.get(i); ... } //#2 for(Iterator it = values.iterator(); it.hasNext(); ) { String

For loop inside For Loop Javascript

▼魔方 西西 提交于 2019-12-12 08:34:43
问题 For some reason this statement is skipping some data.Am I missing a continue statement somewhere or something ? Here is the code for (var i = 0, len = data.ORDER_STATUS[0].ORDERS.length; i < len; i++) { if (data.ORDER_STATUS[0].ORDERS[i].SEC_TYPE == "MLEG") { for (var i = 0; i < data.ORDER_STATUS[0].ORDERS[i].LEGS.length; i++) { LEGS += '<tr class="MLEGS"><td class="orderFirst">' + data.ORDER_STATUS[0].ORDERS[i].LEGS[i].SYMBOL + '</td><td>' + data.ORDER_STATUS[0].ORDERS[i].LEGS[i].ACTION + '<

How to write a simple Java program that finds the greatest common divisor between two numbers? [duplicate]

别等时光非礼了梦想. 提交于 2019-12-12 07:29:23
问题 This question already has answers here : How to find GCD, LCM on a set of numbers (13 answers) Closed 5 years ago . Here is the question: "Write a method named gcd that accepts two integers as parameters and returns the greatest common divisor of the two numbers. The greatest common divisor (GCD) of two integers a and b is the largest integer that is a factor of both a and b. The GCD of any number and 1 is 1, and the GCD of any number and 0 is that number. One efficient way to compute the GCD

bash while loop drops last line of text file [duplicate]

二次信任 提交于 2019-12-12 07:23:03
问题 This question already has answers here : Shell script read missing last line (7 answers) Closed last year . when i cat this file I get 6 lines (it is a diff file) bash-3.00$ cat /tmp/voo 18633a18634 > sashabSTP 18634a18636 > sashatSTP 21545a21548 > yheebash-3.00$ however when i read it line by line i only get 5 lines. bash-3.00$ while read line ; do echo $line ; done < /tmp/voo 18633a18634 > sashaSTP 18634a18636 > sashatSTP 21545a21548 or this bash-3.00$ cat /tmp/voo | while read line ; do