【问题标题】:method to check the value does not work [duplicate]检查值的方法不起作用[重复]
【发布时间】:2015-09-12 12:38:23
【问题描述】:

我想做的是,检查哪个值更大。首先,我(在控制台中)询问第一个值和第二个值。比我想用“更大”的方法检查哪个值更大。问题是,“Bigger”方法带有下划线,我得到一个错误。

错误:

big_than ____ Input.Bigger (big_than ____ Input .)。 " :并非所有代码路径都返回值

错误来自以下方法:

        public int Bigger(Input none)
        {
            if (First > none.Second) 
            {
                return 1;
            }
            if (Second > none.First) 
            {
                return -1;
            }
        }

完整源代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace größer_als____
{
    class Program
    {
        static void Main(string[] args)
        {
            int one;
            int two;

            Console.WriteLine("bigger than...");
            Console.WriteLine("one: ");
            Console.Write(">> ");
                one = int.Parse(Console.ReadLine());
            Console.WriteLine("two: ");
            Console.Write(">> ");
                two = int.Parse(Console.ReadLine());

            Input giveOne = new Input();
            giveOne.First = one;

            Input giveTwo = new Input();
            giveTwo.Second = two;

            if (giveOne.Bigger(giveTwo) == 1) 
            {
                Console.WriteLine("The first one [{0}] is bigger.", giveOne);
            }
            if(giveTwo.Bigger(giveOne) == -1) 
            {
                Console.WriteLine("The second one [{0}] is bigger.", giveTwo);
            }

            Console.ReadLine();
        }
    }
    class Input
    {
        private int first;

        public int First
        {
            get { return first;}
            set { first = value;}
        }

        private int second;

        public int Second
        {
            get { return second; }
            set { second = value; }
        }

        public int Bigger(Input none)
        {
            if (First > none.Second) 
            {
                return 1;
            }
            if (Second > none.First) 
            {
                return -1;
            }
        }
    }
}

【问题讨论】:

  • 你得到了哪个错误,在哪里?
  • 我在“更大”方法中得到错误
  • @echoteck:纠正错误的第一步是读取错误
  • 你得到哪个错误?描述您收到的错误消息
  • 尽管如此,还是有理由创建一个 Input 类?从我的角度来看,您的代码没有多大意义。它可以简单得多。例如每个输入的输入实例,只填充它的一个属性。

标签: c#


【解决方案1】:

如果if 都没有命中怎么办?你需要为每一种可能性提供回报。

    public int Bigger(Input none)
    {
        if (First > none.Second) 
        {
            return 1;
        }
        if (Second > none.First) 
        {
            return -1;
        }
        return 0; // <-- return something here
    }

【讨论】:

  • 好的,非常感谢,我只需要返回一些东西。
【解决方案2】:

并非所有代码路径都返回值

当您使用两个相等的值调用Bigger() 时会发生什么?它不返回任何东西。因此出现错误。

在末尾添加默认条件:

public int Bigger(Input none)
{
    if (First > none.Second) 
        return 1;
    else if (Second > none.First) 
        return -1;
    else
        return 0;
}

【讨论】:

  • 谢谢你们,现在可以了。你帮了我很多!!!!非常感谢!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-15
  • 2020-08-17
  • 2014-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多