我在c#中进行练习,我必须在矩形内移动一个角色。问题是框架右sfasa(见照片)。我没有发现有人可以帮助我的错误?
主类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ricorsione2
{
class Program
{
static void Main(string[] args)
{
World mappa = new World(10,30,4,4);//grande 10X10 parte indice y,x
char cho;
do
{
Console.Clear();
mappa.print();
Console.WriteLine("\n\nDove vuoi andare?");
cho = char.Parse(Console.ReadLine());
mappa.choose(cho);
}
while (true);
Console.ReadLine();
}
}
}
World CLass:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ricorsione2
{
class World
{
int myLung;
int myAltezza;
int myX;
int myY;
public int startX;
public int startY;
public World(int lungArray, int altezzaArray, int xStartded, int yStarted)
{
this.myLung = lungArray;
this.myAltezza = altezzaArray;
this.myX = xStartded;
this.myY = yStarted;
startX = myX;
startY = myY;
}
public void choose(char pos)
{
switch(pos)
{
case'r':
move(1, 0); //x,y
break;
case'l':
move(-1, 0);
break;
case'u':
move(0, -1);
break;
case'd':
move(0, 1);
break;
}
}
public void move(int horizontal, int vertical)
{
myX = (myX + vertical);
myY = (myY + horizontal);
}
public void print()
{
for(int i = 0; i<=myLung;i++)
{
Console.Write('\n');
for(int j=0; j<=myAltezza; j++)
{
if (i == myX && j == myY)
Console.Write('.');
if ((i == 0 || i == myLung) || (j == 0 || j == myAltezza))
Console.Write('*');
else
Console.Write(" ");
}
}
}
}
}
答案 0 :(得分:1)
我找到了,当你打印点.
然后你的代码继续时,你应该使用i
转到下一个continue;
:
将您的代码更改为:
if (i == myX && j == myY)
{
Console.Write('.');
continue;
}
答案 1 :(得分:0)
我想,您应该在else
声明中添加if
声明:
if (i == myX && j == myY)
Console.Write('.');
else if ((i == 0 || i == myLung) || (j == 0 || j == myAltezza))
Console.Write('*');
else
Console.Write(" ");