【发布时间】: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#