gpt4 book ai didi

使用默认值时 switch 语句中的 C# 'Not all code paths return a value'

转载 作者:太空宇宙 更新时间:2023-11-03 17:50:00 25 4
gpt4 key购买 nike

我正在尝试编写一个程序,在用户输入的任何日期之后给出下一个日期。下面的代码是我的 NextDate 类。我收到一条错误消息,指出并非所有代码路径都为我的 GetnextDate 方法返回一个值,即使我已经添加了一个默认值。我真的不知道发生了什么,任何帮助将不胜感激。谢谢:)

namespace NextDate
{
class Date
{
// Variables
private int date;
private int month;
private int year;

// Methods
public Date(int d, int m, int y)
{
this.date = d;
this.month = m;
this.year = y;
}

// Display methods
public override string ToString()
{
string s;

s = "The date is " + this.date.ToString()
+ "/" + this.month.ToString()
+ "/" + this.year.ToString();
return s;
}

private int GetNextDate(int d, int m)
{
switch(m)
{
case 04:
case 06:
case 09:
case 11:
this.date = 01;
break;

default:
this.date = 31;
break;
}
}

}
}

最佳答案

改变

private int GetNextDate(int d, int m)

private void GetNextDate(int d, int m)

关于使用默认值时 switch 语句中的 C# 'Not all code paths return a value',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33945791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com