c#结构函数使用了未赋值的局部变量
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespa...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ex0603
{
class Program
{
struct order
{
public string itemName;
public int unitCount;
public double unitCost;
public double Money()
{
return unitCost * unitCount;
}
}
static void Main(string[] args)
{
order myOrder;
Console.WriteLine("数量:");
myOrder.unitCount = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("单价:");
myOrder.unitCost = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(myOrder.Money());
}
}
}
我编译程序时,出错说最后一行,使用了未赋值的局部变量 myOrder。不知道为什么 展开
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ex0603
{
class Program
{
struct order
{
public string itemName;
public int unitCount;
public double unitCost;
public double Money()
{
return unitCost * unitCount;
}
}
static void Main(string[] args)
{
order myOrder;
Console.WriteLine("数量:");
myOrder.unitCount = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("单价:");
myOrder.unitCost = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(myOrder.Money());
}
}
}
我编译程序时,出错说最后一行,使用了未赋值的局部变量 myOrder。不知道为什么 展开
3个回答
展开全部
实例化:
static void Main(string[] args)
{
order myOrder=new order();
Console.WriteLine("数量:");
myOrder.unitCount = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("单价:");
myOrder.unitCost = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(myOrder.Money());
}
或者可以使用静态:
namespace ex0603
{
class Program
{
public static struct Order
{
public string itemName;
public int unitCount;
public double unitCost;
public double Money()
{
return unitCost * unitCount;
}
}
static void Main(string[] args)
{
Console.WriteLine("数量:");
Order.unitCount = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("单价:");
Order.unitCost = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(myOrder.Money());
}
}
}
static void Main(string[] args)
{
order myOrder=new order();
Console.WriteLine("数量:");
myOrder.unitCount = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("单价:");
myOrder.unitCost = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(myOrder.Money());
}
或者可以使用静态:
namespace ex0603
{
class Program
{
public static struct Order
{
public string itemName;
public int unitCount;
public double unitCost;
public double Money()
{
return unitCost * unitCount;
}
}
static void Main(string[] args)
{
Console.WriteLine("数量:");
Order.unitCount = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("单价:");
Order.unitCost = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(myOrder.Money());
}
}
}
展开全部
static void Main(string[] args)
{
order myOrder =new order();
Console.WriteLine("数量:");
myOrder.unitCount = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("单价:");
myOrder.unitCost = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(myOrder.Money());
}
把order 实例化下
{
order myOrder =new order();
Console.WriteLine("数量:");
myOrder.unitCount = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("单价:");
myOrder.unitCost = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(myOrder.Money());
}
把order 实例化下
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
呵呵,这个很简单的 在声明order 时候 这样写 order myOrder;=new order(); 就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询