[英]C# Console.WriteLine()
So, I wrote this code to run string values in a json file and determine if it is a palindrome or not.所以,我编写了这段代码来在 json 文件中运行字符串值并确定它是否是回文。 I am having trouble displaying the results in the console correctly.我无法在控制台中正确显示结果。 My Console.WriteLine is the issue (i think).我的 Console.WriteLine 是问题(我认为)。 It displays the true/false answers, but I need the actual string to appear with it.它显示真/假答案,但我需要实际的字符串与它一起出现。 For example: "mom: true'. Meaning it is a palindrome. Any tips?例如:“妈妈:真”。意思是它是一个回文。有什么提示吗?
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;
}
}
You should be fine with this:你应该没问题:
pals.result = IsPalindrome(pals.str).ToString();
Console.WriteLine($"{pals.str}: {pals.result}");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.