注册 登录
编程论坛 C# 论坛

C#初学者求助输出问题

z2515832617 发布于 2019-04-30 21:59, 5633 次点击
帮我看一下怎么让输入数组中的数的时候直接显示是数组中的数
输入的数不是数组中的数时直接输出这个数不是数组中的数
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
11 回复
#2
TonyDeng2019-04-30 23:29
數組庫工具有現成函數的
Array.Exists()
你自己查資料

更方便的做法,是用List,不是數組。一般來説,如果沒有充分的理由,都優先使用List。

[此贴子已经被作者于2019-4-30 23:44编辑过]

#3
TonyDeng2019-05-01 00:00
用循環查找是傳統的做法,但現在不需要苦練這種技巧了,Framework中有現成的工具。

對你的問題,具體做法如下:
1.首先Using System.Linq
2.獲取輸入的數shuru後,調用Array.Exists(shu, x => x == shuru),這個函數返回真表示shuru是數組中的數,假則不是。

使用List的做法:
1.直接初始化List數據,或者把數組用.ToList()方法轉換為List
2.求和及查找數據,List都有直接的方法.Sum()和.Contains()

就算是你自己寫傳統代碼,用循環的方法做,也要把查找和求和的動作封裝成函數,讓主程序使用起來跟上面的一樣。
#4
TonyDeng2019-05-01 00:35
仿C版本

using static System.Console;

namespace ConsoleApp1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            int[] shu = { 18, 2, 15, 23, 65, 47 };

            ShowArray(shu);
            WriteLine($"Sum = {Sum(shu)}");
            WriteLine();

            int x;
            while (GetInteger(out x))
            {
                WriteLine(Contain(shu, x) ? "Yes" : "No");
            }
        }

        private static void ShowArray(int[] arr)
        {
            foreach (int item in arr)
            {
                Write($"{item} ");
            }
            WriteLine();
        }

        private static int Sum(int[] arr)
        {
            int sum = 0;
            foreach (int item in arr)
            {
                sum += item;
            }
            return sum;
        }

        private static bool Contain(int[] arr, int x)
        {
            bool found = false;
            foreach (int item in arr)
            {
                if (item == x)
                {
                    found = true;
                    break;
                }
            }
            return found;
        }

        private static bool GetInteger(out int x)
        {
            bool success = false;
            x = 0;
            bool done = false;
            while (!done)
            {
                Write("Input a integer: ");
                string s = ReadLine();
                if (!string.IsNullOrEmpty(s))
                {
                    done = int.TryParse(s, out x);
                    success = done;
                }
                else
                {
                    done = true;
                }
            }
            return success;
        }
    }
}


[此贴子已经被作者于2019-5-1 00:47编辑过]

#5
TonyDeng2019-05-01 00:45
List版本


using System;
using System.Collections.Generic;
using System.Linq;

using static System.Console;

namespace ConsoleApp1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            List<int> shu = new List<int> { 18, 2, 15, 23, 65, 47 };

            ShowArray(shu);
            WriteLine($"Sum = {shu.Sum()}");
            WriteLine();

            int x;
            while (GetInteger(out x))
            {
                WriteLine(shu.Contains(x) ? "Yes" : "No");
            }
        }

        private static void ShowArray(List<int> arr)
        {
            foreach (int item in arr)
            {
                Write($"{item} ");
            }
            WriteLine();
        }

        private static bool GetInteger(out int x)
        {
            bool success = false;
            x = 0;
            bool done = false;
            while (!done)
            {
                Write("Input a integer: ");
                string s = ReadLine();
                if (!string.IsNullOrEmpty(s))
                {
                    done = int.TryParse(s, out x);
                    success = done;
                }
                else
                {
                    done = true;
                }
            }
            return success;
        }
    }
}
#6
z25158326172019-05-01 07:25
回复 5楼 TonyDeng
谢谢谢
#7
东方教主6662019-05-20 13:16
回复 5楼 TonyDeng
学习啦
#8
md000000002019-05-22 17:43
逼逼一句:迭代器比  for + 索引  快
#9
ANAPAEST2019-05-22 19:34
#10
lei_zhisheng2019-06-30 11:33
回复 3楼 TonyDeng
这位兄台说得有道理!给你点个赞!
#11
小磊侠客2019-08-28 13:47
不要后面的for循环,直接用if判断就可以啦
if(shuru==shu[i]){
   Console.WriteLine("这个是数组中的数");
}else{
   Console.WriteLine("这个不是数组中的数");
}
你之所会输出6次,是因为数组[i]是通过下标计算数组的长度,计算出数组长度为6,再加上for循环,循环6次后没有数组中的数,就退出啦
#12
hzx82986842019-12-20 15:39
int[] intArray = { 1, 30, 25, 23, 80, 90 };
         
            
            int sum = 0;
            for (int i = 0; i < intArray.Length; i++)
            {
               
                Console.Write(intArray[i]+"\t" );
                sum += intArray[i];
               
            }
            Console.Write("这个数组的和是{0}",sum+"\n");
            Console.Write("请输入一个整数");
             int shuru= Convert.ToInt32(Console.ReadLine());
            bool chunzai;
            
            chunzai=intArray.Contains(shuru);
            if(chunzai==false)

            Console.Write("这个数不是数组中的数");
                else
                Console.Write("这个数是数组中的数");

            Console.ReadKey();
1