这是一个非常主观的问题,所以我会更具体.有没有时间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) 就像我在标题中说的那样,我在RPG中有一个关于高中的循环.这是主要的循环,它按照时间顺序设置您的一天来执行单个序列.我的问题是我怎么能这样做,以便我检查boolean"节拍"或boolean"丢失"(指游戏的状态)是否在循环中的每个方法之后被跳转到真,但仍然将方法保持在一起环.嵌套在if循环中的if语句是唯一的方法吗?
while (!g.getBeat() || g.getLost())
{
g.wakeUp();
g.goToSchool();
g.beforeLunch();
g.lunchActivity();
g.afterLunch();
g.afterSchool();
g.home();
g.sleep();
}
Run Code Online (Sandbox Code Playgroud) int findMax(int*sums){
int t = 0;
int max = sums[0];
while (sums[t] != '\0'){
printf("current max: %d %d\n", max, sums[t]);
if (sums[t] > max ){
max = sums[t];
}
t ++;
}
return max;
}
Run Code Online (Sandbox Code Playgroud)
这输出:
current max: 7 7
current max: 7 4
current max: 7 2
Run Code Online (Sandbox Code Playgroud)
它忽略了列表的其余部分sums.我认为这是因为下一个元素sums是0.但我不明白为什么它会被0视为'\0'(null).
我有以下Kotlin功能:
fun func(n: Int): Int {
var count = 1
var m = n
while(m != 1) {
m = if(m.isOdd()) 3 * m + 1 else m / 2
count++
}
return count
}
Run Code Online (Sandbox Code Playgroud)
我想用"功能"风格编写这个简单的算法,使用Kotlin的运算符,如map(),count()等.我能想出的最接近的是:
fun func(n: Int): Int {
return n.toList()
.map{ if(it.isOdd()) 3*it+1 else it/2 }
.takeWhile { it != 1 }
.count()
}
Run Code Online (Sandbox Code Playgroud)
显然,上面的代码不起作用,因为map只执行一次,但你知道我想要实现的目标.
PS:toList()只是一个扩展函数,它将int转换为包含该int的列表:
fun Int.toList() = listOf(this)
Run Code Online (Sandbox Code Playgroud) 我正在使用一个
while (true)
{
if (x == y)
{
break;
}
else
{
//do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
像这样循环,该框架只是一个示例框架,因为实际代码本身非常复杂且过于复杂,需要“break;” 在多个不同区域针对多个不同条件进行声明。我的问题是;有没有一种方法可以编写循环语句而不需要循环检查条件?除了 while(true) 之外,还有更有效的方法来编写无限循环吗?
编辑:(java)
编辑2:
while (a < b)
{
while (true)
{
if (c < d)
{
if (e == null)
{
//do alot of stuff
break;
}
else
{
//do something
}
}
else if (d > c)
{
if (e == null)
{
//do alot of stuff
break;
}
else
{
//do something
}
}
else if (d == …Run Code Online (Sandbox Code Playgroud) 输入
\n\n\n(1)汽车满油箱可行驶的最大距离:L公里;
\n(2) 一个整数数组,[0, x1, x2, \xe2\x80\xa6, xn, xn+1],每个整数代表一个位置到源点A的距离。第一个整数为0,即A和A之间的距离。第二个距离x1,表示第一个加油站和A之间的距离。A和B(目的地)之间有n个加油站。xn是最后一个加油站到A的距离,xn+1是B到A的距离。
\n(3)n,为加油站数量。
\n
输出
\n\n\n从 A 到 B 的最少补充次数
\n
代码
\nnumRefills = 0\ncurrentPosition = 0\n\nwhile(currentPosition <= n){\n lastPosition = currentPosition\n\n while(currentPosition <= n && x[currentPosition + 1] \xe2\x80\x93 x[lastPosition] <= L) {\n currentPosition++;\n }\n\n if (currentPosition == lastPosition) return IMPOSSIBLE; \n if (currentPosition <= n) numRefills ++;\n}\n\nreturn numRefills\nRun Code Online (Sandbox Code Playgroud)\n我的疑问是:
\n\n\n
- 为什么上面代码的时间复杂度是O(n)?至少因为嵌套的 while 循环,它不应该是 O(n^2) 吗?
\n- 如何证明“在最远的距离加气”是安全的举动?
\n- 除了使用 …
在java中我们可以使用whileand来编写无限循环for
for(;;){
// doesn't stop[infinite loop]
}
Run Code Online (Sandbox Code Playgroud)
并使用while
while(true){
// doesn't stop[infinite loop]
}
Run Code Online (Sandbox Code Playgroud)
whilekotlin循环创建的语法infinite loop是相同的。
如何使用for循环创建无限循环,就像我上面使用 kotlin 编写的示例(等效代码)一样。
谢谢你!
在检查是否到达字符串末尾时,这两个表达式有什么区别吗?
while(str[i] != '\0')
Run Code Online (Sandbox Code Playgroud)
和
while (str[i])
Run Code Online (Sandbox Code Playgroud)
其中str有类型char*并且i是整数。
while-loop ×10
loops ×4
c ×3
java ×3
kotlin ×2
algorithm ×1
arrays ×1
break ×1
c++ ×1
do-while ×1
equality ×1
expression ×1
for-loop ×1
greedy ×1
if-statement ×1
infinite ×1
int ×1
max ×1
sql ×1
sql-server ×1