给出以下示例代码:
var count = 0;
while (count < 5)
{
using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri))
using (var response = await StaticHttpClient.Client.SendAsync(request))
{
if (!response.IsSuccessStatusCode)
{
switch ((int)response.StatusCode)
{
case 500:
case 504:
continue;
}
}
else
{ ... }
}
count++;
}
Run Code Online (Sandbox Code Playgroud)
这些IDisposable对象是否会在此方法中泄漏内存,还是会正确调用Dispose方法?(交换机中缺少许多情况,我并不关心那里的效率).
我搜索了stackoverflow,但没有发现任何能回答我问题的内容.不幸的是,单词作为搜索查询并不是一个简单的问题.
我正在使用c#,我有一个菜单,要求用户从1 - 4中选择一个选项.我确认所选的值是一个整数,但每当我输入一个字母或符号时我的代码就会中断.如何为此添加验证?我的代码目前如下.
static void Main(string[] args)
{
DirectoryInfo folderInfo = new DirectoryInfo("C:\\Windows");
FileInfo[] files = folderInfo.GetFiles();
int mainMenuChoice=0;
while ( mainMenuChoice != 1 || mainMenuChoice!= 2 || mainMenuChoice!= 3 || mainMenuChoice!= 4)
{
Console.WriteLine("What would you like to do?");
Console.WriteLine("1. Full File Listing.");
Console.WriteLine("2. Filtered File Listing.");
Console.WriteLine("3. FolderStatistics.");
Console.WriteLine("4. Quit.");
mainMenuChoice = int.Parse(Console.ReadLine());
if (mainMenuChoice == 1)
{
Option1();
}
if (mainMenuChoice == 2)
{
Option2();
}
if (mainMenuChoice == 3)
{
Option3();
}
if (mainMenuChoice == …Run Code Online (Sandbox Code Playgroud) 我有一点code我想知道它是否可以写入list comprehension.这while loop部分是我对冷凝感兴趣的部分.
>>> sum=33
>>> final_list=[]
>>> LastCoin=[0, 1, 2, 1, 2, 5, 1, 2, 1, 2, 5, 1, 2,
1, 2, 5, 1, 2, 1, 2, 5, 1, 2, 1, 2, 5, 1, 2, 1, 2, 5, 1, 2, 1]
>>> while sum>0:
... final_list.append(LastCoin[sum])
... sum-=LastCoin[sum]
...
>>> print final_list
[1, 2, 5, 5, 5, 5, 5, 5]
>>>
Run Code Online (Sandbox Code Playgroud) 我不是一个经验丰富的程序员,我的代码有问题,我认为这是我的逻辑错误,但我无法在http://anh.cs.luc.edu/python/hands-on/找到答案3.1/handsonHtml/whilestatements.html.我想要的是检查串行设备是否被锁定,并且"它被锁定"和"它未被锁定"的条件之间的不同之处,,,,在于该行中有4个逗号包含GPGGA字母.所以我希望我的代码启动,如果没有,,,,,但我想我的循环是错误的.任何建议将不胜感激.提前致谢.
import serial
import time
import subprocess
file = open("/home/pi/allofthedatacollected.csv", "w") #"w" will be "a" later
file.write('\n')
while True:
ser = serial.Serial("/dev/ttyUSB0", 4800, timeout =1)
checking = ser.readline();
if checking.find(",,,,"):
print "not locked yet"
True
else:
False
print "locked and loaded"
Run Code Online (Sandbox Code Playgroud)
...
考虑这个脚本
#!awk -f
BEGIN {
"date" | getline foo
print foo
}
Run Code Online (Sandbox Code Playgroud)
它将按预期打印当前日期.但是如果你把它放在一个循环中
#!awk -f
BEGIN {
while (1) {
"date" | getline foo
printf "%s\r", foo
}
}
Run Code Online (Sandbox Code Playgroud)
它只是重复打印相同的日期.我想用一个循环来捕捉外部命令两种getline或system
Python新手在这里很抱歉我确定这是一个愚蠢的问题,但我似乎无法在一个教程中解决以下挑战,该教程要求我使用while循环来检查有效的用户输入.
(使用Python2.7)
这是我的代码,但它无法正常工作:
choice = raw_input('Enjoying the course? (y/n)')
student_surveyPromptOn = True
while student_surveyPromptOn:
if choice != raw_input('Enjoying the course? (y/n)'):
print("Sorry, I didn't catch that. Enter again: ")
else:
student_surveyPromptOn = False
Run Code Online (Sandbox Code Playgroud)
以上打印到控制台:
Enjoying the course? (y/n) y
Enjoying the course? (y/n) n
Sorry, I didn't catch that. Enter again:
Enjoying the course? (y/n) x
Sorry, I didn't catch that. Enter again:
Enjoying the course? (y/n)
Run Code Online (Sandbox Code Playgroud)
这显然是不正确的 - 当用户输入'y'或'n'时循环应该结束但我不知道如何做到这一点.我在这做错了什么?
注意:挑战要求我同时使用!=运算符和loop_condition
在Ruby中,循环似乎与彼此完全相同,while和until循环.
使用一个而不是另一个会有什么情况,为什么Ruby有两个似乎做同样事情的循环?
while循环语法如下:
while conditional [do]
code
end
Run Code Online (Sandbox Code Playgroud)
而直到语法:
until conditional [do]
code
end
Run Code Online (Sandbox Code Playgroud)
所以要尽可能清楚:
$i = 0
$num = 5
while $i < $num do
puts("Inside the loop i = #$i" )
$i +=1
end
Run Code Online (Sandbox Code Playgroud)
和
$i = 0
$num = 5
until $i < $num do
puts("Inside the loop i = #$i" )
$i +=1;
end
Run Code Online (Sandbox Code Playgroud)
将产生两个相同的输出:
Inside the loop i = 0
Inside the loop i = 1
Inside the loop …Run Code Online (Sandbox Code Playgroud) 我最近开始学习Java并在测试时发现了一个问题.这可能是一个非常简单的问题,但我似乎无法解决它.这是我的代码:
int firstj = 1;
if (firstj == 1) {
String choice = "Type a number between 1 and 4";
System.out.println(choice);
while (true) {
if (firstj == 1) {
Scanner third = new Scanner(System.in);
String thirdch = third.nextLine();
while (true) {
if (thirdch.equals("1")) {
System.out.println("Show choice and accept input again ");
System.out.println(choice);
break;
} else if (thirdch.equals("2")) {
System.out.println("Show choice and accept input again ");
System.out.println(choice);
break;
} else if (thirdch.equals("3")) {
System.out.println("Show choice and accept input again ");
System.out.println(choice); …Run Code Online (Sandbox Code Playgroud) 我正在调用一个函数,如果尚未加载网页,它将引发异常。我想等待2秒钟,然后重试,直到页面加载完毕。
我尝试这样:
while(True):
try:
some_funciont()
break
except:
time.sleep(2)
Run Code Online (Sandbox Code Playgroud)
但它在第一次迭代后会逸出。
如果未引发异常,该如何逃生?
我正在尝试创建一个等于列表中每个其他数字之和的函数.例如,如果列表为[0,1,2,3,4,5],则函数应等于5 + 3 + 1.我怎么能这样做?我对Python的了解并没有比while和for循环更远.谢谢.
while-loop ×10
python ×5
c# ×2
loops ×2
awk ×1
boolean ×1
break ×1
continue ×1
exception ×1
for-loop ×1
idisposable ×1
if-statement ×1
java ×1
list ×1
performance ×1
ruby ×1
sum ×1
user-input ×1
validation ×1