Board logo

標題: C# 7 503 取得文字檔長度 [打印本頁]

作者: may    時間: 2024-1-8 22:36     標題: C# 7 503 取得文字檔長度

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. namespace CSA05
  8. {
  9.     class CSA05
  10.     {
  11.         static string[] data = {"Life is difficult, life is unfair, life is challenging and sometimes painful. And life is so very good."
  12.         , "There are frustrations, tragedies, disappointments, setbacks, heartbreaks, and absurdities. The simple joy of being outweighs them all."
  13.         , "Life is good, because within its realm, anything is possible. Life is good, because no matter how far you fall, there is always a way to climb back up again."
  14.         , "You can complain, fret and worry about all the problems in your life, but you'll be wasting your time. Or, you can choose to focus on why and how life is so good, and on what you can do to take that goodness and make it even better."
  15.         , "Not only is life good, it's uniquely good for you in your very own way. The possibilities for expressing your purpose are limited only by your imagination."
  16.         , "Remind yourself often of the great and wonderful value that you already, always have. Life is good, and in this moment that's bursting with possibilities, life is yours."};
  17.         static void Main(string[] args)
  18.         {
  19.             try
  20.             {
  21.                 //TODO
  22.                 int lineNumber = int.Parse(Console.ReadLine());
  23.                 if (lineNumber < 1 || 6 < lineNumber)
  24.                 {
  25.                     throw new ArgumentException();
  26.                 }

  27.                 //    string fileName = "write.txt";
  28.                 //    using (FileStream stream = new FileStream(
  29.                 //        fileName, FileMode.Create, FileAccess.Write))
  30.                 //    {
  31.                 //        using (StreamWriter writer = new StreamWriter(
  32.                 //            stream, Encoding.UTF8))
  33.                 //        {
  34.                 //            for (int i = 0; i < lineNumber; i++)
  35.                 //            {
  36.                 //                writer.WriteLine(data[i]);
  37.                 //            }
  38.                 //        }
  39.                 //    }
  40.                 //}


  41.                 using (StreamWriter writer = new StreamWriter("write.txt", true))
  42.                 {
  43.                     for (int i = 0; i < lineNumber; i++)
  44.                     {
  45.                         writer.WriteLine(data[i]);
  46.                     }
  47.                 }
  48.             }

  49.             catch
  50.             {
  51.                 Console.Write("error");
  52.             }
  53.             Console.ReadKey();
  54.         }


  55.     }
  56. }
複製代碼
TQC+ 物件導向程式語言
最新一次更新時間:2024-01-05 17:41:44

1. 題目說明:
請新增一個主控台應用程式,加入C:\ANS.CSF\CS05資料夾中的CSD05.cs進行編寫。依下列題意進行作答:輸入一個數字代表資料段落數量,將資料寫入至檔案,再輸出大小,使輸出值符合題意要求。檔案名稱請另存新檔為CSA05.cs,儲存於C:\ANS.CSF\CS05資料夾,再進行評分。
請使用正斜線(/)作為檔案路徑的分隔符號。

2. 設計說明:
請撰寫程式,讓使用者輸入一個正整數n(1≦n≦6),專案中已定義名為data的陣列,陣列中有六個元素,將前n個元素的內容寫入write.txt檔案中,每一個元素各自一列,每一次皆覆蓋上一次的資料。若輸入文字或不在範圍內的數字,請輸出【error】。

3. 輸入輸出:
輸入說明
一個正整數n(1≦n≦6)

輸出說明
將前n個元素的內容寫入write.txt檔案中。

範例輸入1
3
範例輸出1
image alt

範例輸入2
5
範例輸出2
image alt

範例輸入3
7
範例輸出3
error

4. 評分項目:
(1) 符合設計說明輸出正確格式        配分20
作者: may    時間: 2024-1-9 09:44

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. namespace CSA05
  8. {
  9.     class CSA05
  10.     {
  11.         static string[] data = {"Life is difficult, life is unfair, life is challenging and sometimes painful. And life is so very good."
  12.         , "There are frustrations, tragedies, disappointments, setbacks, heartbreaks, and absurdities. The simple joy of being outweighs them all."
  13.         , "Life is good, because within its realm, anything is possible. Life is good, because no matter how far you fall, there is always a way to climb back up again."
  14.         , "You can complain, fret and worry about all the problems in your life, but you'll be wasting your time. Or, you can choose to focus on why and how life is so good, and on what you can do to take that goodness and make it even better."
  15.         , "Not only is life good, it's uniquely good for you in your very own way. The possibilities for expressing your purpose are limited only by your imagination."
  16.         , "Remind yourself often of the great and wonderful value that you already, always have. Life is good, and in this moment that's bursting with possibilities, life is yours."};
  17.         static void Main(string[] args)
  18.         {
  19.             try
  20.             {
  21.                 //TODO
  22.                 int lineNumber = int.Parse(Console.ReadLine());
  23.                 if (lineNumber < 1 || 6 < lineNumber)
  24.                 {
  25.                     throw new ArgumentException();
  26.                 }

  27.                 //    string fileName = "write.txt";
  28.                 //    using (FileStream stream = new FileStream(
  29.                 //        fileName, FileMode.Create, FileAccess.Write))
  30.                 //    {
  31.                 //        using (StreamWriter writer = new StreamWriter(
  32.                 //            stream, Encoding.UTF8))
  33.                 //        {
  34.                 //            for (int i = 0; i < lineNumber; i++)
  35.                 //            {
  36.                 //                writer.WriteLine(data[i]);
  37.                 //            }
  38.                 //        }
  39.                 //    }
  40.                 //}


  41.                 using (StreamWriter writer = new StreamWriter("write.txt", true))
  42.                 {
  43.                     for (int i = 0; i < lineNumber; i++)
  44.                     {
  45.                         writer.WriteLine(data[i]);
  46.                     }
  47.                 }
  48.             }

  49.             catch
  50.             {
  51.                 Console.Write("error");
  52.             }
  53.             Console.ReadKey();
  54.         }


  55.     }
  56. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/) Powered by Discuz! 7.2