2014-12-06 3 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

я нашел его, когда вы печатаете точка ., то ваш код продолжается, вы должны перейти к следующему i с помощью continue;:

изменить ваш код:

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

More Read Here

+0

большое спасибо , Я был полезен! :) – user3223680

+0

@ пользователь3223680 ваш приветствуется .. –

Смежные вопросы