저는 대학생 (컴퓨터 과학)이며 방금 C# 프로그래밍 수업을 시작했습니다. 과제를 위해 나는 프로젝트에서 여러 번 사용할 수있는 콘솔 출력을 넣는 "Display"클래스를 사용 해왔다. 예를 들어, 프로그램을 계속하거나 종료하라는 요청입니다. Main()
에 여러 번 입력하는 대신이 메서드를 Display
클래스에서 호출합니다.올바른 클래스 사용?
상위 수준의 다른 학생이 나에게 이렇게해서는 안된다고 말했습니다. 그것은 코딩 방법이 좋지 않으며 기본 클래스 (Main()
포함)에 모든 메서드를 포함하고 절대적으로 필요한 경우에만 다른 클래스를 사용해야합니다.
일부 입력과 조언을 찾고 있습니다.
코드를 포함하도록 요청 받았습니다. 원래는 갈 예정 이었지만이 게시물을 너무 오랫동안 만들고 싶지 않았습니다. 상당히 짧은 한 과제를 선택했습니다. 나는 단지 당신이 많은 사람들이 글을 쓸 수있는 것처럼 코드가 우아하지 않도록 배우는 중임을 분명히 밝힙니다. 건설적인 비판은 대단히 환영합니다.
궁극적으로 저는 수업을 사용하고 있습니다. Display 클래스의 메서드 중 일부는 Main()에서도 쉽게 사용할 수 있습니다.
이
는이 요청 클래스 그 것이다namespace Chapter_6_10
{
// This class is used to get user input
class Request
{
public static void UserInput(out char PrintingCharacter, out int PeakNumber)
{
string input = " ";
char testCharacter = ' ';
int testNumber = 0;
// a do... while loop to get Printing Character from user
// use TryParse() to test for correct input format
do
{
Console.Write("\nPlease enter a character to be used to build triangle : ");
input = Console.ReadLine();
bool result = char.TryParse(input, out testCharacter);
if (result)
{
}
else
{
Console.Clear();
Display.Error(input);
input = " ";
}
}
while (input == " ");
// a do... while loop to get number from user
// use TryParse() to test for correct input format
do
{
Console.Write("\nPlease enter a number <between 1 and 10> for the peak of the triangle : ");
input = Console.ReadLine();
bool result = int.TryParse(input, out testNumber);
if (result)
{
if ((testNumber > 0) && (testNumber < 11))
{
}
else
{
Console.Clear();
Display.Error(testNumber.ToString());
input = " ";
}
}
else
{
Console.Clear();
Display.Error(input);
input = " ";
}
}
while (input == " ");
// assigned received values to 'outs' of method
PrintingCharacter = testCharacter;
PeakNumber = testNumber;
}
}
입니다 홈페이지가 들어있는 프로그램 클래스()
namespace Chapter_6_10
{
class Program
{
static void Main()
{
string triangle = "", result = " ";;
char printingCharacter = ' ';
int peakNumber = 0;
Display.Instructions();
Display.Continue();
// perform a do... while loop to build triangle up to peak
do
{
Console.Clear();
Request.UserInput(out printingCharacter, out peakNumber);
int counter = 1, rowCounter = 0;
do
{
do
{
triangle += printingCharacter;
rowCounter++;
}
while (rowCounter < counter);
counter++;
rowCounter = 0;
triangle += "\n";
}
while(counter != peakNumber);
// perform a do... while loop to build triangle from peak to base
do
{
do
{
triangle += printingCharacter;
rowCounter++;
}
while (rowCounter < counter);
counter--;
rowCounter = 0;
triangle += "\n";
}
while (counter != 0);
Console.Clear();
Console.WriteLine(triangle); // display triangle
Display.DoAgain(out result); // see if user wants to do another or quit
triangle = "";
}
while (result != "q");
}
}
이 Display 클래스
namespace Chapter_6_10
{
// This class displays various outputs required by program
class Display
{
// This method display the instructions for the user
public static void Instructions()
{
Console.WriteLine("\nThis program will ask you to enter a character to be used "
+ " to create triangle."
+ "\nThen you will be asked to enter a number that will represent the"
+ "\ntriangles peak."
+ "\nAfter your values have been received a triangle will be drawn.");
}
// This method displays the choice to continue
public static void Continue()
{
Console.WriteLine("\n\nPress the enter key when you are ready to continue...");
Console.ReadLine();
}
// This method displays an error message
public static void Error(string ErrorType)
{
Console.WriteLine("\nYou have entered \"{0}\", which is a value that is not valid!"
+ "\nThis is not rocket science."
+ "\n\nTry agian...", ErrorType);
}
// This method will ask user to press enter to do again or 'q' to quit
public static void DoAgain(out string Result)
{
string input = " ";
Console.WriteLine("\nPress enter to run program again"
+ "\nor type the letter 'q' to close the application.");
input = Console.ReadLine();
// convert input to lowercase so that only one test needed
Result = input.ToLower();
}
}
에게입니다. 이것이 코드 작성에있어서 비 효과적인 방법으로 간주 될 것인가? 어떻게 개선 할 수 있습니까?
지금까지 모든 입력에 감사드립니다. 그것은 나에게 매우 가치있다.
코드를 공유 할 수 있습니까? 실제로 코드를 보게 될 때 코드에 대해 언급하는 것이 더 쉽습니다. –
아래에서 다루어지기 때문에 이것을 대답으로 넣지는 않겠지 만이 다른 학생이 그 이유 중 하나라고 말하고 싶습니다. 너무 많은 나쁜 코드가 있습니다. 그에게이 링크를 보내고 솔직하게. :) –
나는 Fredrick Mörk에 따라 나의 과제 중 하나에서 코드를 추가했다. 모든 입력을 부탁드립니다. – subcan