我有这个Java代码,使用两种不同类型的循环.
public class Test {
public static void main(String[] args){
long fl = 0, wl = 0;
int i = 0;
int a = 0;
long start = 0, stop = 0;
start = System.currentTimeMillis();
while(i<2000000000){
if(i%2 == 0)
a++;
else
a--;
i++;
}
stop = System.currentTimeMillis();
wl = stop-start/2;
System.out.println("\nWhile loop = "+wl);
i = 0;
a = 0;
start = 0;
stop = 0;
start = System.currentTimeMillis();
for(;i<2000000000;){
if(i%2 == 0)
a++;
else
a--;
i++;
}
stop = …Run Code Online (Sandbox Code Playgroud) 我经常需要做一个做而做的循环。目前,我实现了这样的概念:
Instructions part 1 (for instance: read data)
while(Condition){
Instructions part 2 (save data)
Instructions part 1 (read next data)
}
Run Code Online (Sandbox Code Playgroud)
我必须写两次第1部分,这很丑。有可能摆脱重复吗?我想到这样的概念:
do{
Instructions part 1
} while (Condition) do {
Instructions part 2
}
Run Code Online (Sandbox Code Playgroud) 我正在用java循环做一个阶乘问题,它问1 + 1/2!+ 1/3!+ ... + 1/n!,n是正数,我正在使用"while"来制作它,但代码什么都没有运行:
public static void main(String[] args) {
double sum=0,a=1;
int n=Integer.parseInt(args[0]);
while(n>0){
a=a*n;
sum=sum+1.0/a;
}
System.out.print(sum);
}
Run Code Online (Sandbox Code Playgroud)
请帮忙:)
为什么这个非常简单的Python脚本不起作用?
我熟悉Java,所以我想我会给Python一个...但为什么这不起作用?
def playAgain(roundCounter):
reply = ""
replyList='y n'.split()
if roundCounter == 1:
print('Would you like to play again? Y/N')
while not reply in replyList:
reply = input().lower
if reply == 'y':
roundCounter == 1
elif reply == 'n':
print('Thanks for playing! Bye!')
sys.exit()
Run Code Online (Sandbox Code Playgroud)
这应该打印"你想再玩一次吗?" 然后继续请求用户输入,直到他们输入"Y"或"N".
出于某种原因,它会一遍又一遍地循环,并且不会突破循环 - 即使我键入'y'或'n'.
它是如此简单的一段代码,我不明白为什么它不起作用 - 实际上我在我的脚本中使用了一段几乎相同的代码,它运行良好!
我试图删除最后一次出现'+'后字符串末尾的所有字符.所以例如,如果我的字符串是'母亲+为什么+是+ +河+笑'我想把它减少到'母亲+为什么+是+ +河'.我不知道该字符串将提前是什么.
我想过在字符串上向后迭代.就像是:
while letter in my_string[::-1] != '+':
my_string = my_string[:-1]
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为字母未预先定义.
有什么想法吗?
即使我输入有效的输入并且找不到它的错误,这个循环仍然给我错误信息.它与程序中的其他循环相同,都可以正常工作.有谁知道这个问题?这是循环:
System.out.println("Male or Female (M/F)");
gender = userInput.next().charAt(0);
gender = Character.toLowerCase(gender);
while((gender != 'm') || (gender != 'f')) {
System.out.println("ERROR Please enter a valid age");
System.out.println("Male or Female (M/F)");
gender = userInput.next().charAt(0);
gender = Character.toLowerCase(gender);
}//end while
Run Code Online (Sandbox Code Playgroud)
现在,如果我输入"m"或"f",它会给我错误信息吗???? ???
我有一个评论框,用户可以发表评论.这些注释将在while循环中显示.
<?php
while ($rowsx=mysql_fetch_array($resultss1)){
echo "<div id='comments'>";
echo "<table class='table11'>";
echo "<tr>";
echo "<td class='sss'><img src='img/user.jpg'>";
echo "<p class='p999'>" .$rowsx['username'];
echo "</p> </br>";
echo "<p class='p9date'>" .$rowsx['date_posted']. "</p> ";
echo "</td>";
echo "<td>";
echo "<p class='p9'>" .$rowsx['comment']. "</p> </br></br></br>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "<a href='reply1.php?r_comment=".$rowsx['comment']."&prod_id=".$row['prod_id']."' class='reply_button'> REPLY </a> ";
echo "</div>";
}
?>
Run Code Online (Sandbox Code Playgroud)
现在其他用户也可以回复用户的评论.我想在其他用户的每个评论下面显示他们的回复,但我似乎无法弄清楚如何做到这一点.
我应该在第一个while循环中使用另一个while循环吗?
对不起,如果我使用mysql函数,我知道它已经折旧,但它只是一个学校项目.
这是我显示评论的查询:
$display_comments1="Select username,prod_id,comment,DATE_FORMAT(date_posted, '%m/%d/%Y %H:%i:%s' ) AS date_posted from comment where prod_id='$prod_id' order by date_posted DESC";
$resultss1=mysql_query($display_comments1);
if($resultss1 === FALSE) { …Run Code Online (Sandbox Code Playgroud) 所以我正在写一个程序,有人从牌组中取出牌.所以我写了一个while循环,循环并检查是否有超过4个随机创建的卡,如果有,更改卡.
这是我的代码:
String card = (int)Math.ceil(Math.random() * 13) + " ";
String[] used2 = used.split(" ");
//used is a String like "12 3 7 8 4 ... # etc" such that it is all the previously drawn cards.
boolean checking = true;
boolean isIn = false;
int in = 0;
int check = 0;
while(checking){
for(int q = 0; q < used2.length; q++){
check += 1;
if(card.equals(used2[q] + " ")){
in += 1;
if(in == 4){
System.out.println(check); //debugging line
check …Run Code Online (Sandbox Code Playgroud) 我试图打印出链表中的所有元素.但是,我无法打印出完整的项目清单.
public void display()
{
int count =0;
if (head == null)
{
System.out.println("List is empty");
}
else {
ListNode temp = head;
while (temp.getNext() != null)
{
System.out.println(temp.getElement() +" ");
count ++;
temp = temp.getNext();
}
System.out.println("Total element: "+count);
}
}
Run Code Online (Sandbox Code Playgroud)
//使用的方法
public ListNode getNext()
{
return this.next;
}
/*get the element of Listnode*/
public int getElement()
{
return this.element;
}
Run Code Online (Sandbox Code Playgroud)
请告知哪部分是错误的.
python新手再来一次,
我试图重复删除列表的最后一个条目,直到它返回某个字符.但是尝试运行脚本我得到"IndexError:从空列表中弹出".所以whileList不包含在while循环中?
码:
theList = list("abc/123")
popStop = ""
popped = ""
while popStop != "/":
if theList.pop() != "/":
popped = popped + str(theList.pop())
else:
popStop = "/"
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助.