我正在用Python制作一个基本的文本冒险RPG游戏.我的问题在于移动系统,其中地牢被分成每个都带有坐标的正方形.向玩家询问移动位置的坐标,然后将其传递到函数move2,然后程序检查一堆if语句,类似于下面的语句,并打印地板的相应地图.每个坐标都有一个if语句,因此有40个不同的if语句,每个语句都有一个映射图像.问题是在要求玩家坐标后没有任何反应.程序在询问坐标后结束,但没有给出任何错误(我知道我正在输入正确的坐标.)
move = input("\n To move around the room Hero, simply think the coordinates of the tile you want to move to! However, you can only move one tile at a time Ex: You are at tile 5,4. You can move to 5,3 5,5 or 4,4")
move2(move)
Run Code Online (Sandbox Code Playgroud)
我为糟糕的代码道歉.我确信有一个更好的方法可以做到这一点,但我所知道的还没有......
def move2 (move):
while move == "5,1" "5,2" "5,3" "5,4" "5,5" "5,6" "5,7" "5,8" "4,1" "4,2" "4,3" "4,4" "4,5" "4,6" "4,7" "4,8" "3,1" "3,2" "3,3" "3,4" "3,5" "3,6" "3,7" "3,8" "2,1" "2,2" …Run Code Online (Sandbox Code Playgroud) 我在while循环中遇到if语句时遇到问题.
while pressed == 8 :
print(answerlistx[randomimage], answerlisty[randomimage])
entryx = e1.get()
entryy = e2.get()
answerx = answerlistx[randomimage]
answery = answerlisty[randomimage]
print(entryx, entryy)
if e1 == answerx and e2 == answery:
print("correct")
canvas.delete(images)
randomimage = random.randrange(0,49+1)
scorecounter = scorecounter + 1
game = PhotoImage(file=imagelist[randomimage])
images = canvas.create_image(30, 65, image = game, anchor = NW)
e1.delete(0, END)
e2.delete(0, END)
pressed = ''
else:
print("incorrect")
e1.delete(0, END)
e2.delete(0, END)
pressed = ''
Run Code Online (Sandbox Code Playgroud)
while循环应该检查以查看条目小部件的输入是否与答案匹配,但即使答案正确,它也会转到else语句.我在if语句之前有2个打印语句打印输入,答案只是因为没有它,但它确实正确地显示了它.我还以为它可能与字符串和整数混合在一起所以我将答案列表中的所有答案都改为字符串而没有运气.任何人都能弄清楚它有什么问题吗?提前致谢.
我有类似的东西
while(playAgain==true)
{
cout<<"new game"<<endl; //i know 'using namespace std;' is looked down upon
while(playerCard!=21)
{
*statements*
if(decision=='n')
{
break
}
...
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我想要打破两个循环时,那个中断只会突破第一个while循环
我在SQL Server 2008中有以下SQL语句
DECLARE @Interval table(StartInterval DATETIME,
EndInterval DATETIME )
WHILE (DATEADD(mi,1440,@STARTDATE) <= @ENDDATE)
BEGIN
INSERT INTO @Interval
SELECT @STARTDATE, DATEADD(mi,1440,@STARTDATE)
SET @STARTDATE = DATEADD(mi,1440,@STARTDATE)
END
Run Code Online (Sandbox Code Playgroud)
@StartDate和@EndDate是两个参数.我使用while循环填充表变量,并在我的存储过程中进一步使用此表变量与其他表连接.但我想避免临时表或表变量.有没有什么办法可以在没有任何迭代或循环的情况下使用select查询获得相同的结果(使用while循环实现)?所以我可以做点什么
Select * from (logic that replace while loop) Result inner join some table on col1=col2
Run Code Online (Sandbox Code Playgroud)
期望的结果
StartInterval EndInterval
2013-10-25 00:00:00.000 2013-10-26 00:00:00.000
2013-10-26 00:00:00.000 2013-10-27 00:00:00.000
2013-10-27 00:00:00.000 2013-10-28 00:00:00.000
Run Code Online (Sandbox Code Playgroud) 这段代码是我正在制作的二十一点游戏的片段.无论我输入什么,程序永远不会打破循环.
boolean bool = true;
do{
Scanner kb = new Scanner(System.in);
String choice = kb.next();
if(choice == "Hit" || choice == "hit") {
String action = "hit";
bool = false;
} else if(choice == "Stay" || choice == "stay") {
String action = "stay";
bool = false;
} else {
System.out.println("Do not recognize response. Retry.");
}
} while (bool == true);
Run Code Online (Sandbox Code Playgroud)
通常会发生什么:http: //puu.sh/87KZk.png
任何帮助,将不胜感激.谢谢!
我想知道为什么我们为什么在大多数c程序的末尾都包含一个空的while(1)循环,例如电机驱动器,陀螺仪传感器程序?我搜查了但没有找到任何令人满意的理由.
谢谢.
例如,在ATmega8上的代码:
#include <avr/io.h>
#include <util/delay.h>
void main (void)
{
int n = 1;
DDRB = 0xFF;
for (n=1; n<=10; ++n)
{
PORTB = 0x00;
_delay_ms(1000);
PORTB = 0XFF;
}
while (1)
{}
}
Run Code Online (Sandbox Code Playgroud) 我有一个具有这种结构的数据库:
name|color
paul|blue
mary|red
paul|green
joe |yellow
paul|purple
mary|orange
paul|white
etc |etc
Run Code Online (Sandbox Code Playgroud)
我在这里想要实现的是列出与名称相关的颜色,如下所示:
paul=blue,green,purple,white
mary=red,orange
joe=yellow
Run Code Online (Sandbox Code Playgroud)
我正在检查一些例子并发现了这个:
$query="SELECT * FROM mytable order by name";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['name']. " - ". $row['color'];
echo "<br />";
}
Run Code Online (Sandbox Code Playgroud)
说实话,我只是不知道如何从这个到我想要实现的目标.如何创建一个条件,列出与一个名称关联的所有颜色,然后跳转到下一个,依此类推?
我需要帮助理解这个算法的时间复杂度以及以这种方式迭代的任何算法(i = i*i),我无法弄清楚这个循环与n成比例地执行多少次:它是log n吗?log log n?或类似的东西?我该如何分析这样的事情?提前致谢
function(n):
i=5
while i<n:
{
do.something()
i = i*i
}
Run Code Online (Sandbox Code Playgroud) 也许我现在只是在愚蠢的时刻.我有以下内容:
List<DirectoryEntry> list = new List<DirectoryEntry>();
List.Add(startEntry);
int i=0;
while(i < list.Count) {
DirectoryEntry entry = list[i];
foreach(DirectoryEntry child in GetChildEntries(entry)) {
if(!list.Contains(child)) // Do not add if entry already in list!
list.Add(child);
}
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为Contains以两种方式为指向同一AD对象的directoryEntries返回false.
所以我想从那里创建一个Dictionary并将distinguishedName和条目一起存储:
Dictionary<string,DirectoryEntry> list = new Dictionary<string,DirectoryEntry>();
List.Add(startEntry.Properties["distinguishedName"].Value,startEntry);
int i=0;
while(i < list.Count) {
DirectoryEntry entry = list[i]; // Does not work, because the index is a string here.
// How to get the ith entry from the dictionary?
foreach(DirectoryEntry child in GetChildEntries(entry)) {
var …Run Code Online (Sandbox Code Playgroud) 您好我现在正在使用C#和.NET Framework书进行计算,而我在其中一个练习中遇到了困难
编写C#程序进行更改.输入小于一美元的项目的成本.输出硬币作为更改,使用季度,硬币,镍币和硬币.尽可能使用最少的硬币.例如,如果该项目花费17美分,则变化将是四分之三,一个镍和三个便士
由于我还在尝试掌握c#编程,我提出的最佳方法是使用while循环.
while(costOfItem >= 0.50)
{
costOfItem -= 0.50;
fiftyPence++;
}
Run Code Online (Sandbox Code Playgroud)
我为每个笔20,10,5等都有这些.我正在检查金额是否大于或等于50便士,如果是这样,我从用户给出的金额减少50便士并加1 fiftypence变量.
然后它移动到下一个while循环,我对每个pences都有.问题是,沿着线路的某个环路消失,比如20便士,而costOfItem变成类似"0.1999999999999"的东西,然后它永远不会下降到0,它应该得到正确的变化量.
任何帮助表示赞赏,请不要建议我已经涵盖的复杂程序.