C#으로 기본 데이터 저장 프로그램을 만드는 중입니다. 나는 아주 새롭다, 나에 쉽게 가라. 나는 이것을 다른 클래스로 나눠서 다른 누군가가 자신의 메인 메소드에서 그것을 실행할 수 있도록하고 싶다. 내 문제는 어디에서 시작해야할지 모르겠다. 메서드에 대해 .cs 파일을 추가하려고 시도했지만 배열에 대한 참조와 같은 오류가 프로그램에 생성되었습니다. 여기에 내가 가진 것이있다.두 클래스로 프로그램 나누기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Basic_Item_Entry
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("This program is designed to take input and hold data for 10 items");
//make array for item #'s and add 10 values
Console.WriteLine("Enter 10 item numbers");
int[] itemNums = new int[10];
for(int i = 0; i <itemNums.Length; i++)
{
itemNums[i] = Convert.ToInt32(Console.ReadLine());
}
//make array for item descriptions
string[] itemDesc = new string[10];
for(int i = 0; i < itemNums.Length; i++)
{
Console.WriteLine("Enter the description for item number: " + itemNums[i]);
itemDesc[i] = Console.ReadLine();
}
//add contents of arrays to a file @"C:\temp\DataEntry.txt"
using (System.IO.StreamWriter file = new System.IO.StreamWriter(
@"C:\temp\DataEntry.txt"))
{
file.WriteLine("Item Data:");
for (int i = 0; i < itemNums.Length; i++)
{
file.WriteLine("Item number " + itemNums[i] + " Description: " + itemDesc[i]);
}
file.Close();
}
//finish and maybe print contents from file
Console.WriteLine("Data has been recorded to a file. Would you like to view the the contents? y/n");
//print array data from previously written to file @"C:\temp\DataEntry.txt"
try
{
if (Console.ReadLine().Equals("y"))
{
using (StreamReader stringRead = new StreamReader(@"C:\temp\DataEntry.txt"))
{
String DataEntryTXT = stringRead.ReadToEnd();
Console.WriteLine(DataEntryTXT);
}
}
//dont print anything, just exit (Still creates the file)
else
{
System.Environment.Exit(1);
}
}
catch(Exception ex)
{
Console.WriteLine("File not found");
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
에 그것을 읽을 수있는 설명 논리를 포함합니다. 몇 분만 줘. –
방금 코드를 정리하면 아무 것도 얻지 못할 것입니다. 대신 이것을 시도하십시오. 어디에서든지 "나는 여기있다"라고 생각하면 새로운 생각입니다. 그것을 방법으로 분해하십시오. 사용중인 vars를 전달하고 사용할 결과 값을 반환합니다. *이 클래스의 인스턴스를 만들지 않았기 때문에 메서드 이름 앞에 static을 추가합니다. – FirebladeDan
Dan, 고맙습니다. 답을 원합니다. 어디서부터 시작해야할지 모르겠다. 도움을 청한다. – mattp341