[英]How to read C# . using StreamReader
我正在嘗試使用 StreamReader 讀取字符串,所以我不知道如何讀取它。
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace
{
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";
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 as
{
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
我認為這些行是造成(更多)麻煩的行:
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 行或更多行,您的程序也會中斷。 我把這個放在這里希望能向你澄清一些事情,不過
您的代碼在其當前的 state 中沒有意義。
請閱讀Streams 。
通常你會從一個文件或一個網絡連接中得到一個 stream 而不是從一個字符串中。
您混淆了 integer 和 double。
double
數據類型表示浮點數。
在我看來,您剛剛開始編程並且錯過了大部分基礎知識。
首先,將您的字符串輸入轉換為 stream:
static System.IO.Stream GetStream(string input)
{
Stream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(input);
writer.Flush();
stream.Position = 0;
return stream;
}
現在您可以像這樣將輸入轉換為 stream:
// ... code ...
string itemCostsInput = "25.34\n10.99\n250.22\n21.87\n50.24\n15";
var dataStream = GetStream(itemCostsInput);
// ... code ...
現在您已將字符串輸入轉換為 stream,您可以開始解析數據並提取數字:
static List<double> GetDoubleFromStream(Stream stream)
{
if (stream == null) {
return new List<double>();
}
const char NEWLINE = '\n';
List<double> result = new List<double>();
using (var reader = new StreamReader(stream))
{
// Continue until end of stream has been reached.
while (reader.Peek() > -1)
{
string temp = string.Empty;
// Read while not end of stream and char is not new line.
while (reader.Peek() != NEWLINE && reader.Peek() > -1) {
temp += (char)reader.Read();
}
// Perform another read operation
// to skip the current new line character
// and continue reading.
reader.Read();
// Parse data to double if valid.
if (!(string.IsNullOrEmpty(temp)))
{
double d;
// Allow decimal points and ignore culture.
if (double.TryParse(
temp,
NumberStyles.AllowDecimalPoint,
CultureInfo.InvariantCulture,
out d))
{
result.Add(d);
}
}
}
}
return result;
}
這將是您的中間結果:現在您可以將輸入轉換為 stream,如下所示:
// ... code ...
string itemCostsInput = "25.34\n10.99\n250.22\n21.87\n50.24\n15";
var dataStream = GetStream(itemCostsInput);
var result = GetDoubleFromStream(dataStream);
// ... code ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.