gpt4 book ai didi

c# - 并非所有代码路径都返回值C#错误

转载 作者:行者123 更新时间:2023-12-02 11:10:02 25 4
gpt4 key购买 nike

在我的C#编程类中,我们正在练习创建方法的蛋白质,在我的方法中,我不断在第26行收到“并非所有代码路径都返回值”错误。
我们对此的指示是:

Create a console-based application that computes the price of a desk and whose Main() method calls four other methods: • A method to accept the number of drawers in the desk as input from the keyboard. This method returns the number of drawers to the Main() method. • A method to accept as input and return the type of wood—‘m’ for mahogany, ‘o’ for oak, or ‘p’ for pine. • A method that accepts the number of drawers and wood type, and calculates the cost of the desk based on the following: • Pine desks are $100. • Oak desks are $140. • All other woods are $180. • A $30 surcharge is added for each drawer. •This method returns the cost to the Main() method. • A method to display all the details and the final price. Save the file as Desks.cs



现在,我知道它没有像需求中所述的4种不同的方法,但是我认为我可以缩短它。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Desks.cs
{
class Program
{
static void Main(string[] args)
{
int price = numDrawers() + woodType();
Console.WriteLine("The total price is " + price);
Console.ReadLine();
}
public static int numDrawers()
{

Console.Write("Enter the number of drawers you want with your desk: ");
int drawers = Convert.ToInt32(Console.ReadLine());
int drawerprice = 30 * drawers;
Console.WriteLine("The number of drawers you want is " + drawerprice);
return drawerprice;
}
public static int woodType()
{
char deskWood = 'a';
while(deskWood != 'o' || deskWood != 'p' || deskWood != 'm')
{
Console.Write("Enter the type of wood you would like your desk to be made of \n Oak(o), Pine(p), or Mahogany(m): ");
deskWood = Convert.ToChar(Console.ReadLine());
int woodprice = 0;
if (deskWood == 'o')
{
woodprice += 140;
return woodprice;
}
else if (deskWood == 'p')
{
woodprice += 100;
return woodprice;
}
else if (deskWood == 'm')
{
woodprice += 180;
return woodprice;
}
else
{
Console.WriteLine("There is no wood that corrisponds to that letter.");
}
Console.WriteLine("The type of wood you want the desk to be made of is " + deskWood);
}
}
}

最佳答案

该错误与您的woodType()方法未在所有代码路径上返回整数这一事实相对应。您的程序如下所示:

using System;

public class Program
{
public static void Main()
{
int getWoodTypePrice=woodType();
if(getWoodTypePrice == 0)
{
Console.WriteLine("There is no wood that corrisponds to that letter.");
}
int price = numDrawers() + woodType();
Console.WriteLine("The total price is " + price);
Console.ReadLine();
}

public static int numDrawers()
{

Console.Write("Enter the number of drawers you want with your desk: ");
int drawers = Convert.ToInt32(Console.ReadLine());
int drawerprice = 30 * drawers;
Console.WriteLine("The number of drawers you want is " + drawerprice);
return drawerprice;
}

public static int woodType()
{
char deskWood = 'a';
int woodprice = 0;
while(deskWood != 'o' || deskWood != 'p' || deskWood != 'm')
{
Console.Write("Enter the type of wood you would like your desk to be made of \n Oak(o), Pine(p), or Mahogany(m): ");
deskWood = Convert.ToChar(Console.ReadLine());

if (deskWood == 'o')
{
woodprice += 140;
}
else if (deskWood == 'p')
{
woodprice += 100;
}
else if (deskWood == 'm')
{
woodprice += 180;
}
else
{
//Console.WriteLine("There is no wood that corrisponds to that letter.");
return woodprice;
}
//Console.WriteLine("The type of wood you want the desk to be made of is " + deskWood);
}
return woodprice;
}
}

关于c# - 并非所有代码路径都返回值C#错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61105879/

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