| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1494 人关注过本帖
标题:两个数间的运算
只看楼主 加入收藏
梦幻情缘
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:769
专家分:20
注 册:2005-4-4
收藏
 问题点数:0 回复次数:10 
两个数间的运算
//此程序计算两个数间的运算
using System;
class jisuanqi
{
	public static void Main()
	{
		double a;
		double b;
		string c;
		double sum;
		chongxin: Console.WriteLine("请输入第一个数:");
		a=double.Parse(Console.ReadLine());
		//输入运算符号
		fuha Console.WriteLine("请输入运算符号:");
		c=Console.ReadLine();
		shuru: Console.WriteLine("请输入第二个数:");
		b=double.Parse(Console.ReadLine());
		if (c=="+")
		{
			sum=a+b;
			Console.WriteLine("你所计算的结果是:"+sum);
		}
		else if(c=="-")
		{
			sum=a-b;
			Console.WriteLine("你所计算的结果是:"+sum);
		}
		else if(c=="*")
		{
			sum=a*b;
			Console.WriteLine("你所计算的结果是:"+sum);
		}
		else if(c=="/")
		{
			//注意除数不能为0
			if(b==0)
			{
				Console.WriteLine("你的输入的数据是错误的,除数不能为0.请你重新输入。");
				goto shuru;
			}
			sum=a/b;
			Console.WriteLine("你所计算的结果是:"+sum);	
		}
			//求模运算
		else if(c=="%")
		{
			//注意除数不能为0
			if(b==0)
			{
				Console.WriteLine("你的输入的数据是错误的,除数不能为0.请你重新输入。");
				goto shuru;
			}
			sum=a%b;
			Console.WriteLine("你所计算的结果是:"+sum);
		}
			//判断输入的运算符号是否正确
		else
		{
			//如果运算符号不正确,请重新输入
			Console.WriteLine("你的输入的运算符号是错误的,请重新输入!");
			//返回到fuha
			goto fuhao;
		}
		//设置jixu:
		//输入"Y"或"y"将继续运算,输入"N"或"n"将退出
		jixu:  Console.WriteLine("你是否还要继续呢?Y/N");
		bool i=true;
		while(i)
		{
			string j=Console.ReadLine();
			switch(j)
			{
					//继续运算 
				case "Y":
				case "y":
					goto chongxin;
				case "N":
				case "n":
					//停止运算
					i=false;
					Console.WriteLine("再见了,同志!");
					System.Threading.Thread.Sleep(20);
					break;
				default:
					//如果输入的信息不是"Y"或"y","N"或"n"将返回到jixu:
					Console.WriteLine("你所输入的信息有错误,请重新输入!");
					goto jixu;
			}		
		}
	}
}
搜索更多相关主题的帖子: 运算 
2005-04-06 15:50
yushengou
Rank: 1
等 级:新手上路
帖 子:401
专家分:0
注 册:2005-3-30
收藏
得分:0 
楼主是不是说看了就送分啊

我是初学者,希望大家能多多帮助我 /bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif');}" onmousewheel="return imgzoom(this);" alt="" />
2005-04-06 15:59
梦幻情缘
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:769
专家分:20
注 册:2005-4-4
收藏
得分:0 
我不知道呀,我是刚申请的号呀!
2005-04-06 16:13
yushengou
Rank: 1
等 级:新手上路
帖 子:401
专家分:0
注 册:2005-3-30
收藏
得分:0 
那你为什么要送分呢。
说了送又不送可是不守信的哦

我是初学者,希望大家能多多帮助我 /bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif');}" onmousewheel="return imgzoom(this);" alt="" />
2005-04-06 16:22
梦幻情缘
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:769
专家分:20
注 册:2005-4-4
收藏
得分:0 
对不起呀,我是新手上路呀!
2005-04-06 16:24
yushengou
Rank: 1
等 级:新手上路
帖 子:401
专家分:0
注 册:2005-3-30
收藏
得分:0 
嘎嘎。
知道咯。我好像还没发过这种贴呢。
等会试试。

我是初学者,希望大家能多多帮助我 /bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif');}" onmousewheel="return imgzoom(this);" alt="" />
2005-04-06 16:35
幻风幻云
Rank: 1
等 级:新手上路
帖 子:762
专家分:0
注 册:2005-1-14
收藏
得分:0 
这个也是没调试过的程序吧?

2005-04-07 09:48
梦幻情缘
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:769
专家分:20
注 册:2005-4-4
收藏
得分:0 
调试过了呀,不知为什么贴切上去,就少了些东西.
2005-04-07 10:33
adm_qxx
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2005-4-5
收藏
得分:0 

楼主:在下将您的程序改为不用goto的请过目; 当然限于篇幅其中有些异常未进行处理 相互学习,请多指教.

using System; class Class1 { static void Main() { double a = 0,b = 0,result = 0; string c = null; bool control = true; while(control == true)//控制计算过程 { while(control == true) {//检查除数是否为0 Input(out a,out b,out c); if(b == 0 && (c == "/" || c == "%")) { Console.WriteLine("除数不能是0,请重新输入"); } else control = false; } result = Calculator(a,b,c);//计算 Console.WriteLine("计算结果是:"+result+ "\n你是否还要继续呢?Y/N"); //输出计算结果; //输入"Y"或"y"将继续运算,否则将退出 c = Console.ReadLine(); if(c == "y" || c == "Y") control = true; else control = false; } Console.WriteLine("再见了,同志!"); System.Threading.Thread.Sleep(2000); } static double Calculator(double a,double b,string c) {//a,b为两个操作数,c为操作符; double result = 0; bool control = true;

while(control == true) { switch(c) { case "+": result = a + b; control = false; break; case "-": result = a - b; control = false; break; case "*": result = a * b; control = false; break; case "/": result = a/b; control = false; break; case "%": result = a % b; control =false; break; default: Console.WriteLine("警告:输入操作符错误,请重新输入!"); break; } } return result; } static bool Check(string str) {//检查输入操作符是否合法 string s = "+-*/%"; if(s.IndexOf(str) == -1)//在串s中查找str,如果查不到,则返回-1; { Console.WriteLine("警告:输入操作符错误,请重新输入!"); return false; } else return true; } static void Input(out double a,out double b,out string c) {//out关键字表示引用,当调用方法时, //在方法中对参数所做的任何更改都将反映在该变量中 a = 0; b = 0; c = null;//初始化参数 bool control = true; while(control == true)//输入控制和检查 { Console.WriteLine("请输入两个操作数:"+"\n第一个数:"); a = double.Parse(Console.ReadLine()); Console.WriteLine("第二个数:"); b = double.Parse(Console.ReadLine()); Console.WriteLine("请输入操作符:"+ "\n+:加法;"+ "\n-:减法;"+ "\n*:乘法;"+ "\n/:除法;"+ "\n%:取模;"); c = Console.ReadLine();

if(Check(c) == true) { control = false; } } } }


学习是进步的基础.
2005-04-07 11:09
幻风幻云
Rank: 1
等 级:新手上路
帖 子:762
专家分:0
注 册:2005-1-14
收藏
得分:0 
改得不错!

2005-04-07 11:12
快速回复:两个数间的运算
数据加载中...
 
   



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

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