在C中有一个聪明的技巧,让你通过转向避免金字塔式代码:
if (check1())
if (check2())
if (check3())
do_something();
Run Code Online (Sandbox Code Playgroud)
成:
do {
if (!check1())
break;
if (!check2())
break;
if (!check3())
break;
do_something();
} while (0);
Run Code Online (Sandbox Code Playgroud)
在Python中没有do-while构造的最简洁的方法是什么?
注意:我不一定要求在Python中实现do-while循环的方法,而是一种避免上述金字塔样式代码的技术.
更新:似乎有一些混乱.我使用循环的唯一原因是能够在身体的任何一点突破,这应该只执行一次.
基本上我在Python中做的是这样的:
while True:
if not check1():
break
if not check2():
break
if not check3():
break
do_domething()
break
Run Code Online (Sandbox Code Playgroud)
我只是想知道是否有更清洁的方式.
我想知道而不是使用do-while循环,c中等效的for-loop或任何其他循环组合是什么?
我目前正在处理某人的其他代码,并有这样的声明
if(x.start()) do if(y.foo(x)) {
// Do things
}while(x.inc())
Run Code Online (Sandbox Code Playgroud)
这里x是自定义类,它包含信息y并允许以特殊顺序迭代其元素.如果相关,我会提供此信息,但我的问题更为笼统:
我认为在一个 do{}while()声明中该do部分必须跟随括号,并且这个最后while()条件的togheter 定义了do-while循环.
if后do呢?do和{?我找不到与此相关或谷歌相关的其他问题,大多数内容与将if语句放入内容有关while loop.
我是初学者java并试图解决棘手的问题
输入= 777
输出应为3
7 + 7 + 7 = 21,2 + 1 = 3;
从上面的代码,如果我的输入是333我得到9作为答案,但当总和是两位数(777 = 21)时,我变得空白!
public static void main(String[] args)
{
int y=333;//if y is 777 i am getting blank
int sum=0;
String s;
char []ch;
do
{
s=String.valueOf(y);
ch=s.toCharArray();
if(ch.length>1)
{
for(int i=0;i<ch.length;i++)
{
sum+=Character.getNumericValue(ch[i]);
}
}
else
{
System.out.println(sum);
}
y=sum;
}while(ch.length>1);
}
Run Code Online (Sandbox Code Playgroud) void GasPump::dispense()
{
bool cont = true;
char stop;
do{
cout << "Press any key, or enter to dispense.\n"
<< "Or press 0 to stop: \n";
cin.get(stop);
gasDispensed = gasDispensed + gasDispensedPerCycle;
charges = costPerGallon*gasDispensed;
displayGasNCharges();
if(stop == 0)
cont = false;
} while(cont);
}
Run Code Online (Sandbox Code Playgroud)
做一个作业,这是我第一个用对象写的程序,所以请耐心等待.我只是无法得到这个代码的输出结果是正确的.我需要一种摆脱循环的方法,而我正在使用的是不起作用.有什么建议,提示或提示吗?
我有一些简单的for,while和do-while循环:
while ($var < 1000000) {
++$var;
}
do {
++$var;
} while ($var < 1000000);
for ($var = 0; $var < 1000000; ++$var) {
//do nothing
}
Run Code Online (Sandbox Code Playgroud)
通过比较循环之前和之后的microtime().
do-while循环是相当数量的最快循环.这样做,虽然是比实际速度更快,同时减少了将近一半.我知道他们是出于不同的目的(而检查循环执行前的状态和做,而至少执行一次).
我知道普遍的共识是,虽然循环是不受欢迎的,但更多的是.
我的问题是为什么?考虑到PHP应用程序中使用了多少个for循环,不应该这样做 - 同时使用更多?即使使用if语句在循环执行之前检查条件,性能提升也是相当可观的.
我目前接受的答案是代码易读性是可疑的.
这是交易.我要求监控某些受密码保护的网页以进行更改,并在页面大小与我所知(即已修改)不同时播放声音警报.这是我提出的一大堆代码.
<?php
$e = curl_init();
curl_setopt($e, CURLOPT_URL, 'http://example.com/Account/LogOn');
curl_setopt($e, CURLOPT_POST, 1);
curl_setopt($e, CURLOPT_POSTFIELDS, 'UserName=xxx@example.com&Password=password');
curl_setopt($e, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($e, CURLOPT_REFERER, 'http://example.com');
curl_setopt($e, CURLOPT_RETURNTRANSFER, 1);
curl_exec($e);
$sizevalue = 2399;
do {
curl_setopt($e, CURLOPT_URL, 'http://example.com/panel.php');
$content = curl_exec($e);
$numberofchars = strlen($content);
sleep(15);
} while ($numberofchars = $sizevalue);
curl_close($e);
echo '
<audio controls="controls" autoplay="autoplay">
<source src="/path/to/your/audio.mp3" type="audio/mp3">
<source src="/path/to/your/audio.ogg" type="audio/ogg">
<embed src="/path/to/your/audio.mp3">
</audio>';
php?>
Run Code Online (Sandbox Code Playgroud)
问题1.如果我错了,请纠正我,但据我所知,不是连接服务器,只发布一次用户名和密码,保持连接处于活动状态并使用该auth密钥cookie.txt进行后续操作,它会不断地将用户名和密码发送到每个登录表单.转动周期.我该如何解决?
问题2.我需要脚本给我一些临时状态,因为目前如果不满足某些条件它基本上变成无限循环,托管脚本的服务器给我超时错误.
问题3.实现声音警报的最简单方法是什么?OGG文件播放?Youtube重定向?
为什么我不允许使用我在里面声明的变量do并在whiledo/while循环中使用它?
do
{
index += index;
int composite = index + 1;
// more code here
} while (UnsetBitmask(bitmasks, composite)); // cannot resolve symbol composite
Run Code Online (Sandbox Code Playgroud)
我第一次看到这个,我认为这是一个编译器错误.所以我多次重启Visual Studio但仍然无法识别变量while.
代码有什么问题,或者这是编译器错误?
我试图让一个随机数生成器生成一个1到9之间的数字串,如果它生成一个8,它应该显示最后8,然后停止生成.
到目前为止,它打印1 2 3 4 5 6 7 8,但它不会生成随机的数字串,因此我需要知道如何使循环实际生成如上所述的随机数,感谢您的帮助!
使用Javascript
// 5. BONUS CHALLENGE: Write a while loop that builds a string of random
integers
// between 0 and 9. Stop building the string when the number 8 comes up.
// Be sure that 8 does print as the last character. The resulting string
// will be a random length.
print('5th Loop:');
text = '';
// Write 5th loop here:
function getRandomNumber( upper ) {
var num = Math.floor(Math.random() * …Run Code Online (Sandbox Code Playgroud) 我正在Do While loop我的项目中工作,第一次工作正常.
之前while statement,我为一个数组赋值,我能够在代码底部成功打印数组,但是当我在循环顶部检查时它变为0.
码:
$looparray = array();
$loopend = 0;
$arraymer = array();
$poolafirtsid = $previous_array_values; //previous array values
do {
if (sizeof($looparray) == 0) {
$firstsponarray = $poolafirtsid;
} else {
$firstsponarray = $looparray;
}
$firstsponarray = getUserArray($poolafirtsid);
//get user arraylist of first
foreach ($firstsponarray as $avalue) {
$rooparray = membercount($avalue);
$bsponarray = getUserArray($avalue);
//get second users arraylist 9
if (sizeof($bsponarray > 0)) …Run Code Online (Sandbox Code Playgroud) do-while ×10
loops ×3
php ×3
c++ ×2
automation ×1
c ×1
c# ×1
curl ×1
do-loops ×1
for-loop ×1
java ×1
javascript ×1
performance ×1
python ×1
random ×1
while-loop ×1