我正在尝试使用do ... while循环,它基于我的数组中的id循环.我对使用do while循环有点新,所以在将数组合并到该东西时遇到了一些麻烦.这是我的相关代码:
String studentId = StringUtils.defaultString(request.getParameter("Student_ID"));
String studentId1 = StringUtils.defaultString(request.getParameter("Student_ID1"));
String studentId2 = StringUtils.defaultString(request.getParameter("Student_ID2"));
String studentId3 = StringUtils.defaultString(request.getParameter("Student_ID3"));
String studentId4 = StringUtils.defaultString(request.getParameter("Student_ID4"));
String studentId5 = StringUtils.defaultString(request.getParameter("Student_ID5"));
String studentId6 = StringUtils.defaultString(request.getParameter("Student_ID6"));
String studentId7 = StringUtils.defaultString(request.getParameter("Student_ID7"));
String studentId8 = StringUtils.defaultString(request.getParameter("Student_ID8"));
String studentId9 = StringUtils.defaultString(request.getParameter("Student_ID9"));
String[] studentArray;
studentArray = new String [15];
studentArray[0] = studentId; studentArray[1] = studentId1;
studentArray[2] = studentId2; studentArray[3] = studentId3;
studentArray[4] = studentId4; studentArray[5] = studentId5;
studentArray[6] = studentId6; studentArray[7] = studentId7;
studentArray[8] = studentId8; studentArray[9] = …Run Code Online (Sandbox Code Playgroud) 我在制作这个循环工作时遇到了麻烦,任何帮助/建议都会非常感激.
这是循环(它不是完整的程序):
import random
while your_health or enemy_health >= 10:
print("He is still attacking you.")
print("you may either:"
"a-dodge"
"b-attack back")
fight_choice_1_1 = input("What do you do?")
if fight_choice_1_1 == "a":
d20_1_1 = random.randint(1, 20)
if d20_1_1 >= 10:
print("you dodge the attack")
elif d20_1_1 <= 10:
your_health -= knife_damage
print("you get hit")
if your_health <= 0:
print("you lose. :(")
elif enemy_health <= 0:
print("you win!")
if fight_choice_1_1 == "b":
d20_1_1 = random.randint(1, 20)
if d20_1_1 >= 10:
print("you get …Run Code Online (Sandbox Code Playgroud) 下面的两个代码snipe是否完全相同?
do
{
Delay_us(1);
status = fetchStatus();
}while(!status);
Run Code Online (Sandbox Code Playgroud)
第二个snipet - >
do
{
Delay_us(1);
}while(status = fetchStatus(), !status);
Run Code Online (Sandbox Code Playgroud)
哪个更好?
当我删除&& $f<4它时,下面的代码工作正常,但有了它,它不起作用.
PHP:
$titles=array();
$f=0;
while ($row=mysql_fetch_assoc($query) && $f<4){ //this line doesn't work
$titles[]=$row['questiontitle'];
echo "<div class='questionPreview'>$titles[$f]</div>";
$f++;
}
Run Code Online (Sandbox Code Playgroud) 抱歉这个基本问题.我正在尝试学习Python,但实际上还没有找到答案.
在Python中,我可以为一个while或if语句中的变量赋值吗?如果是这样,怎么样?
例如.我不得不这样做:
line_list = []
while True:
line = raw_input(">>> ")
if not line: break
line_list.append(line)
print line_list
Run Code Online (Sandbox Code Playgroud)
但我想这样做:
line_list = []
while line = raw_input(">>> "):
line_list.append(line)
print line_list
Run Code Online (Sandbox Code Playgroud)
我正在看的教程没有提到这种或那种方式,但这是大多数其他语言的常见结构.
有人可以帮助我做这个循环.当有人输入Y时,循环询问"你有另一个名字".在用户输入N之前,它会不断询问相同的问题.
char ans;
do {
cout<<"Do you have another name (Y/N)?\n";
while(ask!="y"!! ans!!="n") {
cout<<"you must enter 'Y' or an 'N'.\n";
cin >> ans;
}
if(ans=='y')
enter other name();
elseif end;
Run Code Online (Sandbox Code Playgroud) 在我的算法设计和分析讲座中,讲师说for循环将花费更少的时间然后循环用于以下样本算法.
1. for(int i=0;i<5;i++)
{
2. print(i);
}
1. int i=0;
2. while(i<5)
{
3. print(i);
4. i++;
}
Run Code Online (Sandbox Code Playgroud)
他说,编译器会读取1. of for while 5次line 2. 4次因此总时间5 + 4 = 9但是在while循环的情况下.编译器将读取1. 1次,2.5次,3次4次,4次4次.因此总时间1 + 5 + 4 + 4 = 14时请告诉我这是对的.for循环比while循环更快吗?
谢谢.
language-agnostic compiler-construction loops for-loop while-loop
基本上,我试图做一个while循环,并继续阅读,直到线输入是$.当读取它时,循环应该退出.
但是我得到一个运行时异常java.util.InputMismatchException.
这是代码:
while (scan.nextLine() != "$") {
temp1 = scan.nextInt();
temp2 = scan.nextInt();
addEdge(temp1, temp2);
}
Run Code Online (Sandbox Code Playgroud) 我有:
$query = mysql_query("SELECT ... ");
while($row = mysql_fetch_array($query)) { first time used; }
...
while($row = mysql_fetch_array($query)) { second time used; }
Run Code Online (Sandbox Code Playgroud)
第二次它不起作用.为什么?
我想编辑一些用classic-ASP编写的遗留代码.
目前我已经声明了一个使用for-next循环输出一些单选按钮的子程序:
For i = 1 to Cols
response.write "blah"
...
Next
Run Code Online (Sandbox Code Playgroud)
我只是一个计数器,Cols是传递给子程序的值.我尝试将for循环编辑为while循环:
i = Start
do while i <= Cols
response.write "blah"
...
i = i + 1
loop
Run Code Online (Sandbox Code Playgroud)
但是我得到了一个超出响应缓冲区限制的错误.如果我用值替换Cols,它可以正常工作.这是经典ASP的限制吗?
我想使用do while循环的原因是因为当前子例程仅限于从1循环到Cols.有时向后指定循环计数(即步骤-1)会有所帮助,但我不能写:
if Direction = Backwards then
For i = Cols to 1 step -1
else
For i = 1 to Cols
end if
Run Code Online (Sandbox Code Playgroud)