(Hackerrank - Fair Rations)当返回一个字符串时,会出现以下错误(C#). 错误 CS0029: 不能隐式地将string类型转换为int

问题描述 投票:-1回答:2

错误 CS0029:不能隐式地将类型string'转换为int。

我想解决HackerRank的问题。公平口粮. 我应该返回一个Integer或字符串 "NO"。当我试图返回字符串 "NO "时,出现了以下错误信息。

// Solution.cs(49,16): error CS0029: Cannot implicitly convert type `string' to `int'

以下是我的代码。

int temp = 0;
string s = "N0";

for(int i = 0; i < B.Length - 1; i++)
{  
   if(B[i] % 2 == 1)
   {
        B[i] = B[i] + 1;
        B[i + 1] = B[i + 1] + 1;
        temp += 2;
   }
}

for(int i = 0; i < B.Length; i++)
{
    if(B[i] % 2 == 1)
    {
        temp = 0;
    }
}

if(temp > 0)
{
    return temp;
}
else
{
    return s;
}

我如何返回 "NO"?当我使用`Console.WriteLine()'时,出现以下错误信息

//Solution.cs(18,16): error CS0161: `Solution.fairRations(int[])': not all code paths return a value
c# .net string int return
2个回答
1
投票

我想我明白了 何以 你搞混了。 如果你去网站,选择用C#写答案,得到的就是这个模板。

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Solution {

    // Complete the fairRations function below.
    static int fairRations(int[] B) {


    }

    static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        int N = Convert.ToInt32(Console.ReadLine());

        int[] B = Array.ConvertAll(Console.ReadLine().Split(' '), BTemp => Convert.ToInt32(BTemp))
        ;
        int result = fairRations(B);

        textWriter.WriteLine(result);

        textWriter.Flush();
        textWriter.Close();
    }
}

但其中一个要求是:

如果无法做到这一点,请打印出来。NO.

说明书上说:

// Complete the fairRations function below.

这是一个 烂模板 的函数,需要同时处理打印的附加条件。NO 如果解决不了。

你就得 改签fairRations 函数来处理无法解决的情况。 你也可以修改代码中的 Main,这显然不足以满足挑战赛的要求。

如果你需要写一些除 int result 的输出,那么你就需要做一个不同的 textWriter.WriteLine 调用,输出一个字符串。

我强烈推荐在函数中使用的模式,比如 int.TryParseDictionary<TKey,TValue>.TryGetValue. 该模式基于这两个原则。

  1. 返回一个bool来表示成功失败。
  2. 通过一个out参数来传递成功案例的有效载荷(在本例中:一个整数)。

修改 fairRations 这样的函数。

static bool fairRations(int[] B, out int result) {

   if (/* TODO: it is possible? */) {
      result = /* TODO: whatever the result is */;
      return true;
   } else {
      result = 0;
      return false;
   }
}

你可能想修改一下 Main 如下。

改为...

        int result = fairRations(B);

        textWriter.WriteLine(result);

改为...

        if (fairRations(B, out int result)) {
            textWriter.WriteLine(result);
        } else {
            textWriter.WriteLine("NO");
        }

0
投票

打印一个整数taht表示必须分配的最少面包数量,以便每个人都有偶数的面包。如果不能做到这一点,则打印NO。

这个运算结果不容易从C#函数中返回。一个C#函数有 一个 返回值,带 单一 类型。而int和string是突变排斥的返回类型。

除非你愿意去研究复杂而先进的机制,确实允许多个返回值(我建议复制TryParse中使用的模式,用于boolint返回),否则你必须在函数内部用Console.WriteLine()做输出。那个可以给int或字符串。这听起来也比较像你应该做的事情。我可能会在主函数中包含这整个代码,在这一点上没有返回是有意义的。

© www.soinside.com 2019 - 2024. All rights reserved.