【问题标题】:C# Console.WriteLine()C# 控制台.WriteLine()
【发布时间】:2020-01-04 05:15:04
【问题描述】:

所以,我编写了这段代码来运行 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;
    }
}

【问题讨论】:

  • Convert.ToString(json)没有做你认为的那样。

标签: c# palindrome console.writeline


【解决方案1】:

你应该没问题:

pals.result = IsPalindrome(pals.str).ToString();
Console.WriteLine($"{pals.str}: {pals.result}");

【讨论】:

  • 没有用。我有 5 个单独的字符串。妈妈是第一个。我的代码显示 5 行:所有正确或错误的答案。我只需要字符串出现在答案旁边
  • @idkidk 查看我编辑的答案,我理解正确
  • 谢谢,虽然它不识别 $
  • @idkidk 好像你在使用旧的VS,你可以使用这个语法:Console.WriteLine(string.Format("{0}: {1}", pals.str, pals.result));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-08
相关资源
最近更新 更多