代码之家  ›  专栏  ›  技术社区  ›  Nikolay Atanasov

控制台输出系统。尝试将字符串转换为int时的int32[]

  •  0
  • Nikolay Atanasov  · 技术社区  · 7 年前

    我试图通过使用split将它们从string转换为int im,并首次尝试转换,来输出一些与字符串在同一行的数字,输出为system。int32[]

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication2
    {
    class Program
    {
        static void Main(string[] args)
        {
            int Websites = int.Parse(Console.ReadLine());
            int sectok = int.Parse(Console.ReadLine());
            string[] webnamearray = new string[Websites];
            int[] persums = new int[Websites];
            int one = 0;
            int two = 0;
            string datainput = "";
    
            for (int i = 0; i < Websites; i++)
            {
                datainput = Console.ReadLine();
                string[] split = datainput.Split(' ');
                webnamearray[i] = (split[0]);
                one = int.Parse(split[1]);
                two = int.Parse(split[2]);
                persums[i] = one * two;
                Console.WriteLine(persums);
            }
            for (int i = 0; i < Websites;i++)
            {
                Console.WriteLine(webnamearray[i]);
            }
        }
        }
        }
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   Ctznkane525    7 年前

    您写道:

    Console.WriteLine(persums);
    

    这是一个整数数组,这解释了为什么在输出中会看到它。

    相反,我认为您期望的输出是(这是您刚刚创造的价值):

    Console.WriteLine(persums[i]);
    

    如果要将此信息与名称一起放置,请更改上一个循环:

    for (int i = 0; i < Websites;i++)
    {
        Console.WriteLine(webnamearray[i] + " " + persums[i]);
    }