【问题标题】:C# problems if add numbers long.Parse(Console.ReadLine())如果添加数字 long.Parse(Console.ReadLine()) 会出现 C# 问题
【发布时间】:2022-06-12 23:02:44
【问题描述】:

我有下一个任务代码,我需要:

  1. 类型数据元素 - long;
  2. 列表类型 - 双向列表
  3. 如何添加列表项 - 包含在列表末尾
  4. 使用列表操作 - 查找位于偶数位置的 5 倍数的元素数。删除大于平均值的项目

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace labs
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("LabsWork");
            var node = new ListMy();
            long b;
            long suma = 0;
            int counter = 0;
            int av = 0;
            bool alive = true;
            while (alive)
            {
                Console.WriteLine();
                Console.WriteLine("1.Add an item to the end of the list         // 2.Number of multiples of 5 in even positions");
                Console.WriteLine("3.Delete items that are larger than average // 4. Exit the program ");
                Console.WriteLine();

                int command = Convert.ToInt32(Console.ReadLine());
                switch (command)
                {
                    case 1:

                        Console.Write("Input element = ");

                        b = long.Parse(Console.ReadLine());
                        suma = suma + b;
                        counter++;
                        node.push_Back(b);

                        break;
                    case 2:
                        Console.WriteLine();
                        Console.WriteLine("Answer: ", node.CountMultiples().ToString());
                        break;
                    case 3:
                        av = Convert.ToInt32(suma / counter);
                        node.Remove(av);
                        break;
                    case 4:
                        alive = false;
                        continue;


                }
            }
        }
    }
class Node
    {
        public long Value { get; set; }
        public Node Next { get; set; }
        public Node Prev { get; set; }
        public int count { get; set; }
    }
    class ListMy
    {
        private int Size { get; set; }
        private Node Head { get; set; }   //Head referense

        private Node Tail { get; set; }  //Last referense

        public void push_Back(long value)
        {

            Node node = new Node() { Value = value };
            Head.count = 0;
            if (Head == null)
            {
                Head = node;
            }
            else
            {
                node.count = node.Prev.count + 1;
                Tail.Next = node;
                node.Prev = Tail;
            }
            Tail = node;
            Size++;
        }
        public int CountMultiples()
        {
            Node node = Head;
            int k = 0;
            while (node != null)
            {
                if (node.Value % 5 == 0 && node.count % 2 == 0)
                {
                    k++;
                }
                node = node.Next;
            }
            return k;
        }

        public void Remove(int data)
        {
            Node current = Head;

            // search for a removal node
            while (current != null)
            {
                if (current.Value >= data)
                {
                    // if the node is not the last
                    if (current.Next != null)
                    {
                        current.Next.Prev = current.Prev;
                    }
                    else
                    {
                        // if the latter, removed tail
                        Tail = current.Prev;
                    }

                    // if the node is not the first
                    if (current.Prev != null)
                    {
                        current.Prev.Next = current.Next;
                    }
                    else
                    {
                        // if the first, removed head
                        Head = current.Next;
                    }
                }
                current = current.Next;
            }



        }
    }

}

当我尝试添加时,代码工作到了这一点

输入元素 =

我正在尝试输入号码,但下一步我遇到了问题:

如果我在控制台中添加数字 输入元素 = 123 345 213 654 54

异常未处理

System.FormatException: 输入字符串格式不正确

    switch (command)
                    {
                        case 1:
    
                            Console.Write("Input element = ");
    
                            b = long.Parse(Console.ReadLine());
                            suma = suma + b;
                            counter++;
                            node.push_Back(b);
    
                            break;
                        case 2:
                            Console.WriteLine();
                            Console.WriteLine("Answer: ", node.CountMultiples().ToString());
                            break;
                        case 3:
                            av = Convert.ToInt32(suma / counter);
                            node.Remove(av);
                            break;
                        case 4:
                            alive = false;
                            continue;   
    
                    }

b = long.Parse(Console.ReadLine()); 的问题

请帮帮我,我无法运行此代码

【问题讨论】:

  • 在尝试解析之前从输入中删除空格?

标签: c# c#-6.0


猜你喜欢
  • 1970-01-01
  • 2017-10-11
  • 2023-04-04
  • 1970-01-01
  • 2022-06-17
  • 2016-08-08
  • 2012-04-07
  • 2013-04-24
  • 1970-01-01
相关资源
最近更新 更多