c# - C# 控制台.WriteLine()
问题描述
因此,我编写了这段代码来运行 json 文件中的字符串值并确定它是否是回文。我无法在控制台中正确显示结果。我的 Console.WriteLine 是问题(我认为)。它显示真/假答案,但我需要实际的字符串与它一起出现。例如:“妈妈:真”。意思是回文。有什么提示吗?
using System;
using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
//receive json from url
WebClient webClient = new WebClient();
WebClient n = new WebClient();
var json = n.DownloadString("https://raw.githubusercontent.com/bungard/PalindromeTest/master/string.json");
string valueOriginal = Convert.ToString(json);
//parse
Root palindromes = JsonConvert.DeserializeObject<Root>(json);
foreach (Palindromes pals in palindromes.Strings)
{
Console.WriteLine(pals.result = IsPalindrome(pals.str).ToString());**
}
}
public class Root
{
public List<Palindromes> Strings { get; set; }
}
public class Palindromes
{
public string str { get; set; }
public string result { get; set; }
}
public static bool IsPalindrome(string value)
{
char[] forwards = (from c in value.ToLower().ToCharArray() where char.IsLetter(c) select c).ToArray();
int middle = (forwards.Length / 2) + 1;
for (int i = 0; i < middle; i++)
if (forwards[i] != forwards[forwards.Length - 1 - i])
return false;
return true;
}
}
解决方案
你应该没问题:
pals.result = IsPalindrome(pals.str).ToString();
Console.WriteLine($"{pals.str}: {pals.result}");
推荐阅读
- python - 如何在 Windows 中运行使用 pip 安装的程序?
- python - 如何将 R conda env 连接到 jupyter notebook
- r - 循环遍历列以删除 NA
- ios - 将 Xcode 中的搜索限制为自上次提交以来更改的行
- python - How do you call a method both "inside" and "outside" an instance in python?
- mongodb - MongoDB:从字符串字段中删除第一个字符并更新该字段
- laravel - 服务器上的 Laravel 播种机
- express - Express 控制器不返回 cookie
- php - 如何检测 $POST 函数内容是否有空间然后转换为“_”以创建文件夹?
- xcode - Flipper + Flutter 错误:架构 arm64 的未定义符号