2014-12-06 78 views
1

我正在c#中练习,我必须在矩形内移动一个字符。问题是,框架正确sfasa(见照片)。我没有发现有人可以帮助我的错误?我无法正确打印ics的图

主类:

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(); 

     } 





     } 


    } 

世界一流:

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(" "); 






       } 

      } 

     } 

    } 
} 

http://i58.tinypic.com/6ej251.jpg

回答

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(" "); 
1

我发现,当你打印.那么你的代码继续点,你应该使用continue;进入下一i

改变你的代码:

if (i == myX && j == myY) 
{ 
    Console.Write('.'); 
    continue; 
} 

More Read Here

+0

非常感谢,我一直很乐于助人! :) – user3223680 2014-12-06 18:31:11

+0

@ user3223680你的欢迎.. – 2014-12-06 20:35:42