【问题标题】:How to read C# . using StreamReader如何阅读 C#。使用 StreamReader
【发布时间】:2023-01-19 00:26:26
【问题描述】:

我正在尝试使用 StreamReader 读取字符串,所以我不知道如何读取它。

using System;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace Lab3
{
    class Program
    {
        static void Main(string[] args)
        {
            string itemCostsInput = "25.34\n10.99\n250.22\n21.87\n50.24\n15";
            string payerCountInput = "8\n";
            string individualCostInput = "52.24\n";

            byte[] buffer1 = Encoding.UTF8.GetBytes(itemCostsInput);
            byte[] buffer2 = Encoding.UTF8.GetBytes(payerCountInput);
            byte[] buffer3 = Encoding.UTF8.GetBytes(individualCostInput);

            using (StreamReader reader1 = new StreamReader(new MemoryStream(buffer1)))
            using (StreamReader reader2 = new StreamReader(new MemoryStream(buffer2)))
            using (StreamReader reader3 = new StreamReader(new MemoryStream(buffer3)))
            {

                double totalCost = RestaurantBillCalculator.CalculateTotalCost(reader1);
                
                Debug.Assert(totalCost == 433.08);

                double individualCost = RestaurantBillCalculator.CalculateIndividualCost(reader2, totalCost);
                Debug.Assert(individualCost == 54.14);

                uint payerCount = RestaurantBillCalculator.CalculatePayerCount(reader3, totalCost);
                Debug.Assert(payerCount == 9);
            }
        }
    }
}

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

using System.IO;

namespace Lab3
{
    public static class RestaurantBillCalculator
    {

        public static double CalculateTotalCost(StreamReader input)
        {

//  I want to read the input (not System.IO.StreamReader, 

25.34
10.99
250.22
21.87
50.24
15

//below is what i tried..
            int[] numbers = new int[6];
            for (int i = 0; i < 5; i++)
            {
                numbers[int.Parse(input.ReadLine())]++;

            }


            for (int i = 0; i < 5; i++)
            {
                    Console.WriteLine(numbers[i]);
            }


            return 0;
        }
       

        public static double CalculateIndividualCost(StreamReader input, double totalCost)
        {
            return 0;
        }

        public static uint CalculatePayerCount(StreamReader input, double totalCost)
        {
            return 0;
        }
    }
}

即使当我用谷歌搜索时,也只有文件输入/输出出现了那个短语。

我想得到一个简单的字符串并读取它。

            int[] numbers = new int[6]; // The number at the index number

             // take the given numbers
             for (int i = 0; i < n; i++)
             {
                 numbers[int. Parse(sr. ReadLine())]++;
             }

我试过上面的方法,但是没有用。

我只想获取索引并按原样阅读itemCostsInput的内容。如果我只是执行Console.writeLine, String == System.IO.StreamReader

出来我要分别读取和保存itemCostsInput的值。我只想做一些像阅读这样的事情。

对不起,我英语不好

我期待输入阅读

25.34
10.99
250.22
21.87
50.24
15

但是console print System.IO.StreamReader

【问题讨论】:

  • 我没有阅读或测试您的所有代码,但您应该在使用字节数组创建流位置后将其设置回 0。 var str = new MemoryStream(buffer1); str.Position = 0;

标签: c#


【解决方案1】:

我认为这些行是造成(更多)麻烦的行:

        for (int i = 0; i < 5; i++)
        {
            numbers[int.Parse(input.ReadLine())]++;

        }

应该

        for (int i = 0; i < 5; i++)
        {
            numbers[i] = int.Parse(input.ReadLine());

        }

但是由于您有一个小数输入(由于流阅读器而采用字符串格式),因此数字可能应该是一个小数数组。

还有很多关于 StreamReader 使用的评论,因为如果文件没有 5 行或更多行,您的程序也会中断。我把这个放在这里希望能向你澄清一些事情,不过

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 2010-09-21
    • 2016-02-24
    • 2017-02-12
    相关资源
    最近更新 更多