我发现了一个用于家庭作业的Java代码的神秘问题.一个朋友在开始时编写一个应用程序:
public void run() {
vm.setVisible(true);
while(!end);
System.out.println("Finish");
vm.setVisible(false);
}
Run Code Online (Sandbox Code Playgroud)
在所有执行时以及当用户退出应用程序时,布尔'end'为false,这种情况发生:
private class CloseSys implements ActionListener {
public CloseSys() {super();}
public void actionPerformed(ActionEvent e) {
System.out.println("CLOSE SYS");
System.out.println("end: "+end);
end = true;
System.out.println("end: "+end);
}
}
Run Code Online (Sandbox Code Playgroud)
println显示像'end'的值更改为true和逻辑上在我朋友的计算机(MacOS)中完成和应用程序也是如此.
问题是在我的计算机(Ubuntu Linux)中,println也显示了值的变化,但是while没有结束(永远不会达到"Finish"println).有趣的是,如果我们把印刷品放到一边......然后才有效!
简而言之,我正在利用C#进行科学计算,并且我编写了一个方法,该方法具有可以运行到用户指定数量的步骤的while循环......实际上,这种方法可能需要很长时间才能执行(如更多超过5小时).如果需要这么长时间,我可能想要停止方法Esc按键,例如.
当我读到关于破坏的东西时while,它就像Boolean旗帜或类似的东西一样简单.所以我想到这样的事情:
public Double? Run(int n)
{
int i = 0;
while ((i < n) && (/* inputkey != ConsoleKey.Escape */))
{
// here goes the heavy computation thing
// and I need to read some "inputkey" as well to break this loop
i++;
}
// I'm not worried about the return statement, as it is easy to do...
// returns null if the user skipped the method by pressing Escape
// returns null …Run Code Online (Sandbox Code Playgroud) while (foo() == true)
{
foreach (var x in xs)
{
if (bar(x) == true)
{
//"break;" out of this foreach
//AND "continue;" on the while loop.
}
}
//If I didn't continue, do other stuff.
}
Run Code Online (Sandbox Code Playgroud)
我有点坚持如何做到这一点.
更新:我修正了问题.我遗漏了一个事实,即如果我不在continue;while循环上调用,我需要处理其他内容.
对不起,我没有意识到我曾两次使用"某事"这个词.
这是一个非常主观的问题,所以我会更具体.有没有时间do-while循环比正常的while循环更好的编码风格?
例如
int count = 0;
do {
System.out.println("Welcome to Java");
count++;
} while (count < 10);`
Run Code Online (Sandbox Code Playgroud)
在评估do语句(也就是强制do语句至少运行一次)之后检查while条件似乎没有意义.
对于像我上面的例子那样简单的东西,我会想象:
int count = 0;
while(count < 10) {
System.out.println("Welcome to Java"); count++;
}
Run Code Online (Sandbox Code Playgroud)
通常被认为是以更好的写作风格写成的.
任何人都可以为我提供一个工作示例,说明什么时候将do-while循环视为唯一/最佳选择?你的代码中有do-while循环吗?它起什么作用?你为什么选择do-while循环?
(我有一种深刻的感觉,do-while循环可能在编码游戏中有用.如果我错了,请纠正我,游戏开发者!)
我觉得这是一个常见问题,但似乎我在SO或其他网站上找到的答案似乎都没有解决带有计数器的while循环问题.
假设我正在尝试在SQL中编写一个存储过程,该存储过程将填充用户的时间表,方法是在该月的剩余时间内为每一天插入一行.如果@endMonth变量保存了该月的最后一天,那么我知道我可以轻松地编写一个while循环并按照这些行执行某些操作:
WHILE @date <= @endMonth
BEGIN
//Do some action with the date, like an insert
SET @date = DATEADD(d, 1, @date) //increment the date by one day
END
Run Code Online (Sandbox Code Playgroud)
但是,在这里和其他网站上查看答案会让我相信,如果可能的话,最好避免使用while循环.
所以我的问题是:有没有办法在不使用WHILE结构的情况下在SQL中实现带计数器的循环?我将使用什么技术来转换类似于我发布的循环?或者像这样的东西,我是否必须咬紧牙关并使用while循环?
顺便说一下,下面的一些问题很接近,但它们似乎都没有解决需要将计数器作为循环条件的问题.大多数答案似乎都谴责使用WHILE循环,但我似乎无法找到一个替代方案的通用解决方案.
带有日期计数器的sql while循环
SQL Server 2008使用WHILE LOOP插入(这一个很接近,但不幸的是,它只适用于自动增量列)
我浪费时间思考为什么这个程序运行不正常,没有成功.它始终打印"角色是一个特殊的符号".
#include <stdio.h>
int main( void )
{
char character;
printf( "Please type any character and I will tell you what type it is:\n" );
while( 1 )
{
scanf( "%c", &character);
if( character >= 65 && character <= 90 )
printf( "The character is A-Z\n" );
else if( character >= 97 && character <= 122 )
printf( "The character is a-z\n" );
else if( character >= 48 && character <= 57 )
printf( "The character is 0-9\n" );
else …Run Code Online (Sandbox Code Playgroud) 第一次在 Stack Overflow 上发帖,为任何能提供帮助的人喝彩。
我的主程序遇到问题,它应该“从标准输入(或从文件重定向)读取字符串和整数对(字符串后跟 int,每行一对)并将这些对按照读取的顺序插入到最初是空的二叉搜索树。”
使用提供的测试用例测试了二叉搜索树插入和遍历本身后,我知道我的插入和遍历工作。但是,我很难在同一行上同时读取 string 和 int,并且不确定如何实现文件重定向(我可以在将其上传到的 UNIX 服务器上使用 cat 命令吗?)。
这是我的 main.c
#include <stdio.h>
#include "bst.h"
int main(void)
{
BStree bst;
int size;
char quit;
char *str;
int num;
printf("Please enter the size of the tree: ");
scanf("%d", &size);
bst = bstree_ini(size);
printf("Please enter the first key (str) & data (int) you wish to enter, separated by whitespace: ");
while ((scanf(" %s %d", str, &num)) == 2) {
bstree_insert(bst, *str, num);
printf("Please enter Q/q if you …Run Code Online (Sandbox Code Playgroud) #!/bin/bash
# slightly malformed input data
input_start=2014-11-1
input_end=2016-01-1
# After this, startdate and enddate will be valid ISO 8601 dates,
# or the script will have aborted when it encountered unparseable data
# such as input_end=abcd
startdate=$(date -I -d "$input_start") || exit -1
enddate=$(date -I -d "$input_end") || exit -1
m="$startdate"
while [ "$m" != "$enddate" ]; do
echo $m
m=$(date -I -d "$m + 1 month")
done
Run Code Online (Sandbox Code Playgroud)
“脚本运行良好,但当我想更改 While 循环条件即 '<=' '小于或等于' 时,即使我尝试使用“-le”,也会出现错误。我在这里要做的是 startdate <= enddate在 while 循环中。任何人都可以建议需要做什么来克服这个问题。
我对在 Tensorflow 中实现递归神经网络感兴趣,就像如何在 TensorFlow 中实现递归神经网络?。
然而,在他的实现中,该语句parallel_iterations的tf.while_loop被固定为 1。我担心这可能太慢了。由于我要输入张量流的树具有彼此不依赖的部分,因此我希望可以设置parallel_iterations为更高的值。然而,我作为张量流的输入输入的树中不可避免地需要一些依赖项,并且我担心将其设置为更高的值可能会破坏依赖项属性。
所以我的问题是,Tensorflow 是否tf.while_loop已经自动捕获了依赖关系,以便仅在彼此不依赖的位置上使用并行性?
张量流文档说明如下:
对于正确的程序,while_loop 对于任何并行迭代 > 0 都应返回相同的结果。
但我不确定他们所说的“正确的程序”是什么意思。
我有一个运行多次迭代的代码,只有满足条件时,才会保存迭代的结果。这自然地表示为 while 循环。我试图使代码并行运行,因为每个实现都是独立的。所以我有这个:
while(nit<avit){
#pragma omp parallel shared(nit,avit)
{
//do some stuff
if(condition){
#pragma omp critical
{
nit++;
\\save results
}
}
}//implicit barrier here
}
Run Code Online (Sandbox Code Playgroud)
这工作正常...但是每次实现之后都会存在一个障碍,这意味着如果我在并行块内所做的事情在一次迭代中比其他迭代花费的时间更长,那么我的所有线程都在等待它完成,而不是继续下一次迭代。
有没有办法避免这种障碍,使线程继续工作?我平均进行了数千次迭代,所以再进行几次迭代也不会造成什么影响(如果变量nit在已经运行的线程中没有增加)...
我试图将其变成并行的 for,但是 for 循环中的自动增量使变量nit变得疯狂。这是我的尝试:
#pragma omp parallel shared(nit,avit)
{
#pragma omp for
for(nit=0;nit<avit;nit++){
//do some stuff
if(condition){
\\save results
} else {
#pragma omp critical
{
nit--;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,它继续工作并围绕 for 循环进行,但我的nit变量采用不可预测的值...正如人们可以从不同线程在不同时间增加和减少它所期望的那样。
我还尝试将 for 循环中的增量留空,但它无法编译,或者试图欺骗我的代码,使其在 for 循环中没有增量,例如
...
incr=0;
for(nit=0;nit<avit;nit+=incr)
...
Run Code Online (Sandbox Code Playgroud)
但后来我的代码崩溃了......
有任何想法吗?
谢谢 …
while-loop ×10
break ×2
c ×2
c# ×2
for-loop ×2
java ×2
loops ×2
bash ×1
c++11 ×1
compare ×1
continue ×1
date ×1
do-while ×1
if-statement ×1
input ×1
linux ×1
openmp ×1
python-2.7 ×1
scanf ×1
shell ×1
sql ×1
sql-server ×1
tensorflow ×1
user-input ×1