Turtle Graphics - 进程因 StackOverflowException 而终止
Turtle Graphics - Process terminated due to StackOverflowException
我正在尝试用 C# 准备一个 Turtle 图形解决方案。一切正常,但由于 WhosebugException,它以 Process Terminated 结束。我检查了 this
和
问题是 getter setter 或无限循环。但是我的代码中没有任何这种情况。我是 C# 的新手。
下面是我的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TurtleGraphics
{
class Program
{/**
* Directions: 0 right, 1 down, 2 left, 3 up
*/
private static short direction = 0;
private static bool penDown;
private static int turtleX = 0, turtleY = 0;
private static int[,] floor = new int[20, 20];
public static void Main(String[] args)
{
initFloor(floor);
//Scanner in = new Scanner(System.in);
printMenu();
int nextCommand = int.Parse(Console.ReadLine());
while (nextCommand != 9)
{
switch (nextCommand)
{
case 1:
penDown = false;
break;
case 2:
penDown = true;
break;
case 3:
direction++;
break;
case 4:
direction--;
break;
case 5:
Console.WriteLine("How many steps do you want to move?");
int move = int.Parse(Console.ReadLine());
if (move <= 10)
while (--move != 0)
Moves();
break;
case 6:
printArray();
break;
default:
Console.WriteLine("Unknow command, please try again:\n");
break;
}
Moves();
Console.WriteLine("What's next?");
nextCommand = int.Parse(Console.ReadLine());
}
}
private static void initFloor(int[,] floor)
{
for (int i = 0; i < floor.GetLength(0); i++)
{
for (int j = 0; j < floor.GetLength(1); j++)
{
floor[i,j] = 0;
}
}
}
private static void printMenu()
{
Console.WriteLine("Commands List:\n\n\t1 Pen up\n"
+ "\t2 Pen down\n"
+ "\t3 Turn right\n"
+ "\t4 Turn left\n"
+ "\t5 to 10 Move forward 10 spaces (replace 10 for a different number of spaces)\n"
+ "\t6 Display the 20-by-20 array\n"
+ "\t9 End of data (sentinel)Please enter a command number:\n");
}
private static void printArray()
{
for (int i = 0; i < floor.GetLength(0); i++)
{
for (int j = 0; j < floor.GetLength(1); j++)
{
// Console.WriteLine(floor[i, j]);
// Console.WriteLine(" ");
if (floor[i, j] == 0)
Console.Write(".");
else if (floor[i, j] == 1)
Console.Write("*");
else if (floor[i, j] == 2)
Console.Write("T");
}
Console.WriteLine();
}
}
private static void Moves()
{
switch (direction)
{
case 0:
turtleX++;
break;
case 1:
turtleY++;
break;
case 2:
turtleX--;
break;
case 3:
turtleY--;
break;
default:
if (direction < 0)
direction = 3;
else
direction = 4;
Moves();
break;
}
if (penDown)
{
if (turtleX < 20 && turtleY < 20)
floor[turtleX, turtleY] = 1;
else
{
direction -= 2;
Moves();
}
}
}
}
}
感谢任何快速帮助。谢谢
有一种明显的情况是您最终会无休止地循环。假设您正在输入 Moves
方法并且 direction
是 4:
private static void Moves()
{
switch (direction)
{
case 0:
//Nope
case 1:
//Nope
case 2:
//Nope
case 3:
//Nope
default:
if (direction < 0)
//Nope
else
direction = 4; //Okay, but it already was
Moves(); //And call myself again
代码无法取得任何进一步的进展,调用堆栈被越来越多的永远不会完成的 Moves()
调用填满。
我无法提供更正,因为我不明白您的代码的用途。将其分解为 更小 方法,方法名称 更清晰 和 XML 描述其作用的文档注释。然后确保代码和注释一致。
我不知道名为 Moves
的方法的正确 操作是什么。
Moves 中 switch 语句的默认部分应该是这样的:
default:
if (direction < 0)
direction += 4;
else
direction -= 4;
Moves();
break;
因为它的目的是包装方向值,使其始终保持在 0-3 范围内。
我正在尝试用 C# 准备一个 Turtle 图形解决方案。一切正常,但由于 WhosebugException,它以 Process Terminated 结束。我检查了 this
和
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TurtleGraphics
{
class Program
{/**
* Directions: 0 right, 1 down, 2 left, 3 up
*/
private static short direction = 0;
private static bool penDown;
private static int turtleX = 0, turtleY = 0;
private static int[,] floor = new int[20, 20];
public static void Main(String[] args)
{
initFloor(floor);
//Scanner in = new Scanner(System.in);
printMenu();
int nextCommand = int.Parse(Console.ReadLine());
while (nextCommand != 9)
{
switch (nextCommand)
{
case 1:
penDown = false;
break;
case 2:
penDown = true;
break;
case 3:
direction++;
break;
case 4:
direction--;
break;
case 5:
Console.WriteLine("How many steps do you want to move?");
int move = int.Parse(Console.ReadLine());
if (move <= 10)
while (--move != 0)
Moves();
break;
case 6:
printArray();
break;
default:
Console.WriteLine("Unknow command, please try again:\n");
break;
}
Moves();
Console.WriteLine("What's next?");
nextCommand = int.Parse(Console.ReadLine());
}
}
private static void initFloor(int[,] floor)
{
for (int i = 0; i < floor.GetLength(0); i++)
{
for (int j = 0; j < floor.GetLength(1); j++)
{
floor[i,j] = 0;
}
}
}
private static void printMenu()
{
Console.WriteLine("Commands List:\n\n\t1 Pen up\n"
+ "\t2 Pen down\n"
+ "\t3 Turn right\n"
+ "\t4 Turn left\n"
+ "\t5 to 10 Move forward 10 spaces (replace 10 for a different number of spaces)\n"
+ "\t6 Display the 20-by-20 array\n"
+ "\t9 End of data (sentinel)Please enter a command number:\n");
}
private static void printArray()
{
for (int i = 0; i < floor.GetLength(0); i++)
{
for (int j = 0; j < floor.GetLength(1); j++)
{
// Console.WriteLine(floor[i, j]);
// Console.WriteLine(" ");
if (floor[i, j] == 0)
Console.Write(".");
else if (floor[i, j] == 1)
Console.Write("*");
else if (floor[i, j] == 2)
Console.Write("T");
}
Console.WriteLine();
}
}
private static void Moves()
{
switch (direction)
{
case 0:
turtleX++;
break;
case 1:
turtleY++;
break;
case 2:
turtleX--;
break;
case 3:
turtleY--;
break;
default:
if (direction < 0)
direction = 3;
else
direction = 4;
Moves();
break;
}
if (penDown)
{
if (turtleX < 20 && turtleY < 20)
floor[turtleX, turtleY] = 1;
else
{
direction -= 2;
Moves();
}
}
}
}
}
感谢任何快速帮助。谢谢
有一种明显的情况是您最终会无休止地循环。假设您正在输入 Moves
方法并且 direction
是 4:
private static void Moves()
{
switch (direction)
{
case 0:
//Nope
case 1:
//Nope
case 2:
//Nope
case 3:
//Nope
default:
if (direction < 0)
//Nope
else
direction = 4; //Okay, but it already was
Moves(); //And call myself again
代码无法取得任何进一步的进展,调用堆栈被越来越多的永远不会完成的 Moves()
调用填满。
我无法提供更正,因为我不明白您的代码的用途。将其分解为 更小 方法,方法名称 更清晰 和 XML 描述其作用的文档注释。然后确保代码和注释一致。
我不知道名为 Moves
的方法的正确 操作是什么。
Moves 中 switch 语句的默认部分应该是这样的:
default:
if (direction < 0)
direction += 4;
else
direction -= 4;
Moves();
break;
因为它的目的是包装方向值,使其始终保持在 0-3 范围内。