伊莉討論區

標題: 小弟又有作業問題了 不好意思! (C#的陣列) [打印本頁]

作者: asdfg0205    時間: 2016-11-23 05:17 PM     標題: 小弟又有作業問題了 不好意思! (C#的陣列)

本帖最後由 asdfg0205 於 2016-11-23 05:22 PM 編輯

這週只有7題 @@  剩下了 這兩題
第五題 好像好複雜阿.... 想不到
第六題  請問  輸入數字 我當然會寫拉~都學那麼久了  可是  (能夠輸入數字 又能夠輸入逗點、分號、空白隔開)
該怎麼寫

不好意思喔! 我作業發問了好多次  也感謝熱心的大大幫助  謝謝!  
(作業好像都越來越刺激了.....)

作者: Josie_2016    時間: 2016-11-24 08:59 AM

本帖最後由 Josie_2016 於 2016-11-24 09:08 AM 編輯

第五題
  1. int[] x1 = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  2. int[] x2 = new int[10] {   2,  4,  6,  8,10,12,14,16,18,20 };
  3. int[] y1 = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  4. int[] y2 = new int[10] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
  5. int x = 5, y = 5;//點
  6. for (int i = 0; i < x1.Length; i++)
  7.             {
  8.                 if (x > x1[i] && x < x2[i] && y > y1[i] && y < y2[i])
  9.                 {
  10.                     Console.WriteLine(i);
  11.                 }
  12. }
複製代碼
第六題
  1. string input = Console.ReadLine();
  2. string[] temp = input.Split(new char[] { ' ', ';', ',' });
  3. string[] temp1 = temp.Distinct().ToArray();
  4. foreach (string s in temp1)
  5. {
  6.                 Console.WriteLine(s + "count:" + temp.Where(n => n == s).Count());
  7. }
複製代碼

作者: asdfg0205    時間: 2016-11-26 04:10 PM

本帖最後由 asdfg0205 於 2016-11-26 04:16 PM 編輯
Josie_2016 發表於 2016-11-24 08:59 AM
第五題第六題

謝謝喔!!!  不好意思 可以請問您一下第六題   Distinct().ToArray(); 的作用是什麼嗎?
像是我打上 Distinct() 然後去微軟查  找的了這個Enumerable.Distinct 因為前面又+了Enumerable
我不知道我是否找對 但是我也看不太懂他在說什麼....
像是 我打 C# ToArray() 去搜尋又找到了 List<T>.ToArray  那他前面的這個List<T> (裡面的T代表著???)又是有甚麼作用

不好意思

作者: Josie_2016    時間: 2016-11-26 07:14 PM

  1. string[] temp1 = temp.Distinct().ToArray();
複製代碼
上面的意思是從temp取不重複的項目並轉成陣列放到temp1

List<T>的T代表泛型,如List<int>的T是int,List<string>的T是string,意思就是可以放任何型別進List,是集合的概念,您可以把它看成陣列的加強版,將來老師應該會教因為很重要
作者: 跨越青春嶺    時間: 2016-11-26 07:42 PM

asdfg0205 發表於 2016-11-26 04:10 PM
謝謝喔!!!  不好意思 可以請問您一下第六題   Distinct().ToArray(); 的作用是什麼嗎?
像是我打上 Distinc ...

首先要先了解 Distinct() 的原型,它提供了 "靜態方法" 與 "擴充方法",而 "擴充方法" 是針對 IEnumerable 做擴充,this IEnumerable<TSource> 那段
  1. using System.Collections;
  2. using System.Collections.Generic;

  3. namespace System.Linq
  4. {
  5.     //
  6.     // 摘要:
  7.     //     提供一組 static (在 Visual Basic 中為 Shared) 方法,用於查詢實作 System.Collections.Generic.IEnumerable`1
  8.     //     的物件。
  9.     public static class Enumerable
  10.     {
  11.             //
  12.         // 摘要:
  13.         //     使用預設的相等比較子來比較值,以便從序列傳回獨特的項目。
  14.         //
  15.         // 參數:
  16.         //   source:
  17.         //     要移除重複項目的序列。
  18.         //
  19.         // 類型參數:
  20.         //   TSource:
  21.         //     source 之項目的型別。
  22.         //
  23.         // 傳回:
  24.         //     System.Collections.Generic.IEnumerable`1,其中包含來源序列中的獨特項目。
  25.         //
  26.         // 例外狀況:
  27.         //   T:System.ArgumentNullException:
  28.         //     source 為 null。
  29.         public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source);
  30.     }
  31. ]
複製代碼


範例
  1. using System;
  2. using System.Linq;

  3. namespace ConsoleApplication3
  4. {
  5.     internal class Program
  6.     {
  7.         private static void Main(string[] args)
  8.         {
  9.             //宣告這個陣列目前很多重複的數字
  10.             var number = new int[] { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5 };
  11.             //方法一:使用 System.Linq 提供的擴充方法
  12.             var result = number.Distinct();
  13.             Console.WriteLine(string.Join(" ", result));
  14.             //方法二:使用 System.Linq 提供的 static class Enumerable 使用裡面的 Distinct
  15.             var result2 = Enumerable.Distinct(number);
  16.             Console.WriteLine(string.Join(" ", result2));
  17.             //所以方法一二都可以達到目的
  18.             Console.ReadKey();
  19.         }
  20.     }
  21. }
複製代碼



作者: vihee    時間: 2016-11-26 09:31 PM

沒依題目指示要用陣列,反用了LIST來儲存座標

我創了一類別Shape
  1. public class Shape
  2.     {

  3.         public Shape(int x1,int y1,int x2,int y2)
  4.         {
  5.             coordinate_X = x1;
  6.             coordinate_Y = y1;
  7.             coordinate_X2 = x2;
  8.             coordinate_Y2 = y2;
  9.         }


  10.         int coordinate_X = 0;
  11.         int coordinate_Y = 0;
  12.         int coordinate_X2 = 1;
  13.         int coordinate_Y2 = 1;

  14.         public bool isRange(int pointX, int pointY)
  15.         {
  16.             bool range = false;
  17.             if (compareX(pointX))
  18.             {
  19.                 range = compareY(pointY);
  20.             }
  21.             return range;
  22.         }

  23.         private bool compareX(int pointX)
  24.         {
  25.             bool range = false;
  26.             if (coordinate_X >= coordinate_X2)
  27.             {
  28.                 range = coordinate_X2 <= pointX && coordinate_X >= pointX;
  29.             }
  30.             else
  31.             {
  32.                 range = coordinate_X <= pointX && coordinate_X2 >= pointX;
  33.             }
  34.             return range;
  35.         }

  36.         private bool compareY(int pointY)
  37.         {
  38.             bool range = false;
  39.             if (coordinate_Y >= coordinate_Y2)
  40.             {
  41.                 range = coordinate_Y2 <= pointY && coordinate_Y >= pointY;
  42.             }
  43.             else
  44.             {
  45.                 range = coordinate_Y <= pointY && coordinate_Y2 >= pointY;
  46.             }
  47.             return range;
  48.         }
  49.     }
複製代碼
用此類別負責矩形的各參數,並在類別內寫了比較
先比較X軸,若X軸沒在範圍內,Y軸就不進行比較
  1. static void Main(string[] args)
  2.         {
  3.             List<Shape> listRectangle = new List<Shape>(10);
  4.             listRectangle.Add(new Shape(1, 3, 4, 1));
  5.             listRectangle.Add(new Shape(1, 4, 5, 1));
  6.             listRectangle.Add(new Shape(1, 6, 6, 1));
  7.             listRectangle.Add(new Shape(1, 8, 7, 1));
  8.             listRectangle.Add(new Shape(1, 10, 8, 1));
  9.             listRectangle.Add(new Shape(11, 12, 9, 1));
  10.             listRectangle.Add(new Shape(13, 14, 10, 1));
  11.             listRectangle.Add(new Shape(15, 16, 11, 1));
  12.             listRectangle.Add(new Shape(17, 18, 12, 1));
  13.             listRectangle.Add(new Shape(19, 20, 13, 1));

  14.             Console.Write("請輸入2個整數座標(以空白分隔):");
  15.             string[] point = Console.ReadLine().Split(' ');
  16.             int pointX = int.Parse(point[0].Trim());
  17.             int pointY = int.Parse(point[1].Trim());
  18.             StringBuilder resultText = new StringBuilder();
  19.             resultText.Append(String.Format("({0},{1}) 落入以下矩形: ", pointX, pointY));
  20.             int rectangleIndex = 0;
  21.             foreach (Shape rectangle in listRectangle)
  22.             {
  23.                 rectangleIndex++;
  24.                 if (rectangle.isRange(pointX, pointY))
  25.                 {
  26.                     resultText.Append(rectangleIndex + " ");
  27.                 }
  28.             }
  29.             Console.Write(resultText.ToString());
  30.             Console.ReadLine();
  31.         }
複製代碼
當作是另一種寫法提供你參考




歡迎光臨 伊莉討論區 (http://www40.eyny.com/) Powered by Discuz!