我正在阅读关于如何编程网络套接字的文章,并遇到了这段代码:
try {
while (true) { // This is the line in question
int i = in.read( );
if (i == -1) break;
System.out.write(i);
}
}
catch (SocketException e) {
// output thread closed the socket
}
catch (IOException e) {
System.err.println(e);
下面的代码声称使用带有标志的do循环来重复输入,直到获得有效的int。
do
{
try
{
// attempt to convert the String to an int
n = Integer.parseInt( s );
goodInput = true;
}
catch ( NumberFormatException nfe )
{
s = JOptionPane.showInputDialog( null,
s + " is not an integer. Enter an int
第4行和第5行让我感到悲哀:
1 def test_break_statement
2 i = 1
3 result = 1
4 while true
5 break unless i <= 10
6 result = result * i
7 i += 1
8 end
9 assert_equal 3628800, result
10 end
我不确定在while true语句中哪些内容需要保持正确,但是我相信是后面的代码。这导致了更多的混乱,因为我正在阅读这行代码:
如果i不小于或等于10,则break
在python中的while循环中工作吗?我似乎不能让它起作用。这是我的代码如何工作的示例。
newslot = 3
moved = False
while newslot > 0 or moved != True:
enabled = query something on the database where slot = newslot
if enabled:
print 'do something here'
moved = True
else:
newslot-=1
prin
a = input('Enter your username...')
while (a == 'SuperGiraffe007&'):
print('Welcome!')
else:
print("Try again...")
a = input('Enter your username...')
while (a == 'SuperGiraffe007&'):
print('Welcome!')
代码功能正确,但当我输入正确的用户名时,字符串“欢迎
我试着更深入地理解while循环。我理解基本原则,即只要测试条件为真,就会重复一段时间的语句。
在“加速C++”一书中,作者声明如下:
//the number of blanks surrounds the greeting
const int pad = 1;
//total number of rows to write
const int rows = pad * 2 + 3;
//we have written r rows so far
int r = 0;
//setting r to 0 makes the invariant true
while (r != rows)
我正在寻找关于如何使用多个条件来编写while循环的建议。主要的思想是,我们有一些条件是经过检查的,如果它们不符合要求,它们就会被重复。例如,我们需要输入一些输入(两个数字的数字字符串)。输入必须是数字,必须不少于3,并且必须有相同的数字。如果不满足任何条件,它会通知用户并再次请求输入。如果输入符合所有要求,则循环停止。最好的方案是什么?我的想法是这样的:
while (true) {
if (!(someMethod)) {
print("This doesnt meet the condition. Try again!");
con
我在客户端和服务器之间有一个套接字连接。我可以连接和发送数据,但它不断重复相同的数据,而不是在读取一行后停止/暂停。
预期输出:
Reading: ls /home
实际输出:
Reading: ls /homeReading: ls /homeReading: ls /homeReading: ls /homeReading: ls /homeReading: ls /homeReading: ls /homeReading: ls /homeReading: ls /homeReading: ls /homeReading: ls /homeReading: ls /homeReading
我试图让我的程序在用户输入y/n时重复,但是我对如何在这种类型的输入中使用while true感到困惑,下面是一些代码。
again = input("Would you like to play again? enter y/n: ")
if again == "n":
print ("Thanks for Playing!")
quit
if again == "y":
print ("Lets play again..")
????
此外,如果用户输入了不同的字符,但考虑到
我正在尝试编写一个代码,它不断地要求用户输入。一旦重复这些输入之一,while循环就会中断并打印重复输入的一行。到目前为止,我的代码如下:
ls = []
i = 1
while True:
user = input("Give me an input:")
ls.append(user)
if user in ls:
print("Input number {} is not unique!".format(user))
i += 1
但是,当我运行代码时,它并没有按照我希望的方式执行:
[user@sahara
最近,由于我在编译器设计方面的研究,我开始用不同的语言编译器做一些实验,
我发现了一件很奇怪的事,那发生在Java里
也就是说,如您所知,如果我们有一个没有返回路径的方法,则不需要返回任何值。
所以,如果我有下一个方法,这很好,因为这是一个无限循环。
private int Run(){
while (true){
}
}
但是,一旦我在任何地方都可以使用break语句,即使在不可能的条件下,non-reachable area也会抱怨编译。
因此,下一个java代码将不会编译
private int Run(){
while
为什么在C#中我得到的结果是84而不是63,而这两个代码在我看来是一样的?我做错了什么?为什么?
首先是微软c#教程中的代码,结果是63:
int sum = 0;
for (int number = 1; number < 21; number++)
{
if (number % 3 == 0)
{
sum = sum + number;
}
}
Console.WriteLine($"The sum is {sum}");
下面是我的代码,结果是84:
int Num1 = 0;
int Num2 = 0;
do
{
Num1++;
while pyautogui.locateOnScreen('white4x4.png', region = (200,200,4,4)) != None:
playsound('click.mp3')
if pyautogui.locateOnScreen('white4x4.png', region = (200,200,4,4)) == None:
continue
pyautogui拍摄了一个截图,检查我的图像是否在某个区域,我希望它继续重复。
playsound('click.mp3')
这可能是非常主观的,但是当控制变量在循环中更新时,人们通常如何在C#中布局他们的循环控制?我身上的书呆子不喜欢单独的声明和重复。例如:
string line = reader.ReadLine();
while (line != null)
{
//do something with line
line = reader.ReadLine();
}
我的C程序员想把它改成
while (string line = reader.ReadLine() != null)
{
//do something with line
}
但是C#的表达式似乎不是这样工作的:(
在第33行,有一个中断来阻止代码无限期地重复,但我希望它能够参与while loop。
代码:
#include <iostream>
using namespace std;
int main()
{
while (true){
{
cout << "This program counts by twos to any number that is inputted by the user." << endl;
cout << "Input an even number to st
如果空行是重复的,也应该删除它们。如果行有转义序列(如\t),则与空行不同。下面的代码删除了太多的行,或者有时会留下重复的行。如何解决这个问题?
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[6000];
char b[6000];
int test = 0;
fgets(a, 6000, stdin);
while (fgets(b, 6000, stdin) != NULL) {
for (int i = 0; i < 6000;
我有一个问题:如何将新游戏放入代码或PlayAgain (y)或(n)这是我的学校项目。我一直试图给自己找到一个解决方案,但它只会重复问题“再次播放”或错误。
import random
player1 = input("Enter your real name: ")
player2 = "Computer"
state = random.randint(12,15)
print("The number of sticks is " , state)
while(True):
我刚开始学习Python。基本上,如果答案为是,我只想重复循环一次,或者如果答案为否,则中断循环。返回True/False不会返回到while循环吗?
def userinfo():
while true:
first_name=input("Enter your name here:")
last_name=input("Enter your last name:")
phonenum=input("Enter your phone number here")
inputagain=rawinput(
有人知道这段代码有什么问题吗?即使在输入超过1行之后,程序也将在代码的末尾过早结束,而不允许嵌套时重复。谢谢。
#include <stdio.h>
int main() {
int line;
unsigned int sum = 0;
int input;
float average;
printf("Enter number of input lines:");
scanf("%d", & line);
while (line
我正在做一个类似的游戏,在小基础乒乓球。我想做的是,当球击中墙壁,游戏结束,并显示错误信息。
我所做的是使用was循环如下:
While (hits right wall) or (hits left wall) or (hits top wall) or (hits bottom wall) = "True"
GraphicsWindow.ShowMessage("you lost", "game over")
Endwhile
实际上,这样做是不断重复错误信息,我必须退出程序。我如何才能让它只显示一次信息,当它击中任何一面墙的条件都
我在一本书上看到这个:
BubbleSort( list )
length <-- lenght of list
do {
swapped_pair <-- false
index <-- 1
while index <= length - 1 {
if list[index] > list[index + 1] {
swap( list[index], list[index + 1] )
s
从C#换成python后,我正在学习python。我不明白为什么我的代码不能工作。 import MyFunctions
def main():
game = True
while game:
trials = 1
number = MyFunctions.Generate_random_number()
print('Próba numer:', trials)
userNumber = int(input('Podaj liczbe z przedzialu od 1 do 100
我的代码要求我先执行底部的代码行,然后执行顶部的代码行。下面是我的代码:
test = True
while test:
if test==False:
print("Now executing top lines of the code")
break
elif test==True:
print("Now executing bottom lines of the code")
test = False
continue
我的预期输出:
Now executing bottom
下面的程序来自于python的一本书。在此代码中,首先将计数设置为0,然后使用while True。在这本书中,我看到零和空字符串被计算为False,而所有其他值都被计算为True。如果是这样的话,那么程序如何执行while循环?当计数设置为0时,计数不被计算为False吗?
有人能解释一下吗?
# Finicky Counter
# Demonstrates the break and continue statements
count = 0
while True: # while count is True
count += 1
# end loo