指错
using System;using System.Collections.Generic;
using System.Text;
namespace lable3_1
{
class rectangle
{
private object x, y;
public
int area(int x, int y)
{
return x * y;
}
float area(float x, float y)
{
return x * y;
}
double area(double x,double y)
{
return x*y;
}
static void compare(int r1, double r2,float r3)
{
if(r1>r2)
{
return r1;
if(r1>r3)
return r1;
else
return r3;
}
else
{
return r2;
if(r2>r3)
return r2;
else
return r3;
}
static void Main(string[] args)
{
rectangle.r1 =new rectangle();
rectangle.r2 = new rectangle();
rectangle.r3 = new rectangle();
Console.WriteLine("请输入r1的长和宽:");
x = Convert.ToInt32(Console.ReadLine());
y = Convert.ToInt32(Console.ReadLine());
r1 = area(intx, inty);
Console.WriteLine("{0}",r1);
Console.WriteLine("请输入r2的长和宽:");
x = Convert.ToDouble(Console.ReadLine());
y = Convert.ToDouble(Console.ReadLine());
r2=area(Double x,Double y);
Console.WriteLine("{0}",r2);
Console.WriteLine("请输入r3的长和宽:");
x=Convert.ToSingle(Console.ReadLine());
y=Convert.ToSingle(Console.ReadLine());
r3=area(float x,float y);
Console.WriteLine("{0}",r3);
}
}
}
--------------------编程问答-------------------- 运行的时候有很多错误 --------------------编程问答-------------------- --------------------编程问答-------------------- 咋做哇?愁 --------------------编程问答-------------------- 错误主要在Main函数中
首先实例化对象有问题:
rectangle.r1 =new rectangle();
——》(不要点“.”)
rectangle r1 =new rectangle();
然后,静态函数中不能事件访问实例变量:
x = Convert.ToInt32(Console.ReadLine());
y = Convert.ToInt32(Console.ReadLine());
x,y为rectangle类的实例变量,需要用对象引用去点,比如:r1.x r1.y
然后是方法的调用,实例方法在静态函数中需要用对象引用去点。
比如: r1.area(intx, inty); 而且这里你的intx,intx没有定义
在然后area()函数返回的是一个数字,不能直接赋值给对象引用r1
还有方法的逻辑有一些问题。
--------------------编程问答-------------------- 下面可以参考
using System;
using System.Collections.Generic;
using System.Text;
namespace lable3_1
{
class rectangle
{
static int area(int x, int y)
{
return x * y;
}
static float area(float x, float y)
{
return x * y;
}
static double area(double x,double y)
{
return x*y;
}
static void compare(int r1, double r2,float r3)
{
Console.WriteLine();
if(r1>r2)
{
Console.WriteLine(r1);
if(r1>r3)
Console.WriteLine(r1);
else
Console.WriteLine(r3);
}
else
{
Console.WriteLine(r2);
if(r2>r3)
Console.WriteLine(r2);
else
Console.WriteLine(r3);
}
}
static void Main(string[] args)
{
Console.WriteLine("请输入r1的长和宽:");
int x = Convert.ToInt32(Console.ReadLine());
int y = Convert.ToInt32(Console.ReadLine());
int r1 = area(x,y);
Console.WriteLine("{0}",r1);
Console.WriteLine("请输入r2的长和宽:");
double x1 = Convert.ToDouble(Console.ReadLine());
double y1 = Convert.ToDouble(Console.ReadLine());
double r2=area(x1,y1);
Console.WriteLine("{0}",r2);
Console.WriteLine("请输入r3的长和宽:");
float x2=Convert.ToSingle(Console.ReadLine());
float y2 =Convert.ToSingle(Console.ReadLine());
float r3=area(x2,y2);
Console.WriteLine("{0}",r3);
compare(r1, r2, r3);
}
}
}
--------------------编程问答-------------------- 除 --------------------编程问答-------------------- 这些类里的函数为什么都要用Staic 来修饰啊? --------------------编程问答-------------------- 可以帮我用你的这种方法编写完这个程序吗?谢谢啦 --------------------编程问答-------------------- --------------------编程问答--------------------
class rectangle--------------------编程问答-------------------- 谢谢 --------------------编程问答-------------------- int a = r1.area(x, y);这句里面不可以 int r1=r1.area(x,y);吗? --------------------编程问答-------------------- 上面写错了 r我说的是这个 1=r1.area(x,y); --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 除
{
//private object x, y;
//私有变量定义在这里没什么用
//这里3个area方法构成重载
public int area(int x, int y)
{
return x * y;
}
float area(float x, float y)
{
return x * y;
}
double area(double x, double y)
{
return x * y;
}
//static void compare(int r1, double r2, float r3)
//既然下面有return语句,这里就必须定义返回值
static double compare(int r1, double r2, float r3)
{
//求3个数字中的最大值,可以使用数组的Max方法
double[] nums = new double[] { r1, r2, r3 };
//由于参数中有 int float double 所以我们直接返回最大的类型double
return nums.Max();
//也可以使用if判断的方式,不过你这个if逻辑没对
//if (r1 > r2)
//{
// return r1;
// if (r1 > r3)
// return r1;
// else
// return r3;
//}
//else
//{
// return r2;
// if (r2 > r3)
// return r2;
// else
// return r3;
//}
}
static void Main(string[] args)
{
//这里定义类型的时候没有点“.”
rectangle r1 = new rectangle();
rectangle r2 = new rectangle();
rectangle r3 = new rectangle();
Console.WriteLine("请输入r1的长和宽:");
//这里的x y需要定义
int x = Convert.ToInt32(Console.ReadLine());
int y = Convert.ToInt32(Console.ReadLine());
//area是实例方法,需要用引用加.去调用
//由于输入的是两个int型的变量,调用的是int参数的重载,返回int值
int a = r1.area(x, y);
Console.WriteLine("{0}", a);
//后面调用double和float一样的道理
}
}
补充:.NET技术 , C#