| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1043 人关注过本帖
标题:刚学c# 书后练习遇到疑问 总是编译不通过 请帮我看下错在哪
只看楼主 加入收藏
易碎务碰
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-4-14
收藏
 问题点数:0 回复次数:6 
刚学c# 书后练习遇到疑问 总是编译不通过 请帮我看下错在哪
我是刚刚开始学c#的 教科书后有这样一题 用控制台应用程序写一个 + - * /的4则运算计算器
编写完后总是编译不通过 希望各位大虾能指点下我
程序如下

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication6
{

    class mac
    {
        public int Num1;
        public int Num2;
        public int Da;
        public char Oper;

        public void Jia()
        {
            Num1 + Num2 = Da;
            System.Console.WriteLine(Da);
        }

        public void Jian()
        {
            Num1 - Num2 = Da;
            System.Console.WriteLine(Da);
        }

        public void Chen()
        {
            Num1* Num2 = Da;
            System.Console.WriteLine(Da);
        }

        public void Chu()
        {
            Num1 / Num2 = Da;
            System.Console.WriteLine(Da);
        }

    }
    class Program
    {



        static void Main(string[] args)
        {
            mac c = new mac();
            Console.WriteLine("input the num1");
            c.Num1=Convert.ToInt32(Console.ReadLine());
           

             Console.WriteLine("input the oper");
            c.Oper=Convert.ToChar(Console.ReadLine());


             Console.WriteLine("input the num2");
            c.Num2=Convert.ToInt32(Console.ReadLine());


            if (c.Oper==+)
            {
                c.Jia;
            }

            else if (c.Oper==-)
            {
                c.Jian;
            }

            else if (c.Oper==*)
            {
                c.Chen;
            }

            else if (c.Oper==/)
            {
                c.Chu;
            }
        }
    }
}
搜索更多相关主题的帖子: 书后 计算器 疑问 public using 
2008-04-14 20:04
sswin0922
Rank: 2
等 级:论坛游民
帖 子:25
专家分:10
注 册:2008-3-10
收藏
得分:0 
public da 应该是你存储结构的变量吧!=号是赋值语句,你这么写Num1 / Num2 = Da,等于是吧 da的默认值副给了 Num1 / Num2 ! 这个肯定是不对的!
还是有最好是能把编译的错误结果发上看看比较能看清楚
2008-04-14 21:26
buchiputao
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-3-27
收藏
得分:0 
回复 1# 的帖子
我也是刚刚接触c#,这是我在你那基础上改了些代码,运行出结果了,代码如下:
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication12
{
    class mac
    {
        public int Num1;
        public int Num2;
        public int Da;
        public char Oper;

        public void Jia()
        {
            Da = Num1 + Num2;
            System.Console.WriteLine(Da);
            Console.ReadKey();
        }

        public void Jian()
        {
            Da = Num1 - Num2;
           Console.WriteLine(Da);
           Console.ReadKey();
            
        }

        public void Chen()
        {
             Da=Num1* Num2 ;
            Console.WriteLine(Da);
            Console.ReadKey();
        }

        public void Chu()
        {
           Da =Num1 / Num2  ;
            Console.WriteLine(Da);
            Console.ReadKey();
        }

    }
    class Program
    {



        static void Main(string[] args)
        {
            mac c = new mac();
            Console.WriteLine("input the num1");
            c.Num1=Convert.ToInt32(Console.ReadLine());
           

             Console.WriteLine("input the oper");
            c.Oper=Convert.ToChar(Console.ReadLine());


             Console.WriteLine("input the num2");
            c.Num2=Convert.ToInt32(Console.ReadLine());


            if (c.Oper=='+')
            {
                c.Jia();
            }

            else if (c.Oper=='-')
            {
                c.Jian();
            }

            else if (c.Oper=='*')
            {
                c.Chen();
            }

            else if (c.Oper=='/')
            {
                c.Chu();
            }
        }
    }

}
2008-04-15 13:36
易碎务碰
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-4-14
收藏
得分:0 
谢谢你啊 方便留个qq嘛以后大家交流交流啊呵呵
2008-04-18 21:03
易碎务碰
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-4-14
收藏
得分:0 
我的81478731
2008-04-18 21:03
天琛
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-7-25
收藏
得分:0 
我也是刚学的,刚接触。看看我的对不!
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication16
{
    class Program
    {
        class yunsuan
        {
            public int a, b, c;
            public char F;
            public void jia()
            {
                c = a + b;

            }
            public void jian()
            {
                c = a - b;

            }
            public void cheng()
            {
                c = a * b;

            }
            public void chu()
            {
                c = a / b;

            }
        }
        public static void Main()
        {
            yunsuan q = new yunsuan();
            aa:Console.WriteLine("请输入被加数/被减数/被乘数/被除数:");
            q.a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入你想进行的运算符号:");
            q.F=Convert.ToChar( Console.ReadLine());
            Console.WriteLine("请输入加数/减数/乘数/除数:");
            q.b = Convert.ToInt32(Console.ReadLine());

            if (q.F == '+')
                q.jia();
            else if (q.F == '-')
                q.jian();
            else if (q.F == '*')
                q.cheng();
            else if (q.F == '/')
                q.chu();
            Console.WriteLine("计算结果为:");
            Console.WriteLine("{0}" + q.F + "{1} = {2}", q.a, q.b, q.c);
            goto aa;
        }
    }
}
2008-04-23 15:15
足迹
Rank: 1
来 自:广东惠州
等 级:新手上路
帖 子:46
专家分:0
注 册:2008-4-2
收藏
得分:0 
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication6
{

    class mac
    {
        public int Num1;
        public int Num2;
        public float Da;
        public char Oper;

        public void Jia()
        {
            Da = Num1 + Num2;
            //Num1 + Num2 = Da;
            System.Console.WriteLine(Da);
        }

        public void Jian()
        {
            Da = Num1 - Num2;
            //Num1 - Num2 = Da;
            System.Console.WriteLine(Da);
        }

        public void Chen()
        {
            Da = Num1 * Num2;
            //Num1* Num2 = Da;
            System.Console.WriteLine(Da);
        }

        public void Chu()
        {
             Da = (float)Num1 / Num2;
            //Num1 / Num2 = Da;
            System.Console.WriteLine(Da);
        }

    }
    class Program
    {



        static void Main(string[] args)
        {
            mac c = new mac();
            Console.WriteLine("input the num1");
            c.Num1=Convert.ToInt32(Console.ReadLine());
           

             Console.WriteLine("input the oper");
            c.Oper=Convert.ToChar(Console.ReadLine());


             Console.WriteLine("input the num2");
            c.Num2=Convert.ToInt32(Console.ReadLine());


            if (c.Oper=='+')
            {
                c.Jia();
            }

            else if (c.Oper=='-')
            {
                c.Jian();
            }

            else if (c.Oper=='*')
            {
                c.Chen();
            }

            else if (c.Oper=='/')
            {
                c.Chu();
            }
            Console.ReadLine();
        }
    }
}
最好把Da声明为float类型  因为你用到了除法。
2008-04-23 16:19
快速回复:刚学c# 书后练习遇到疑问 总是编译不通过 请帮我看下错在哪
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019003 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved