簡體   English   中英

C# Console.WriteLine()

[英]C# Console.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}");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM