Console.WriteLine("计算:\n请输入一个两个数的算式进行计算(按回车即可出结果):\r")
string r = Console.ReadLine()
int y = 0 //储存字符串长度
double Num1
double Num2
foreach (char t in r)
{
if (t == '+')
{
Num1 = Convert.ToDouble(r.Substring(0, y))
Num2 = Convert.ToDouble(r.Substring(y+1, r.Length - y))
Console.WriteLine(Num1 + Num2)
break
}
else if (t == '-')
{
Num1 = Convert.ToDouble(r.Substring(0, y))
Num2 = Convert.ToDouble(r.Substring(y+1, r.Length - y))
Console.WriteLine(Num1 - Num2)
break
}
else if (t == '*')
{
Num1 = Convert.ToDouble(r.Substring(0, y))
Num2 = Convert.ToDouble(r.Substring(y+1, r.Length - y))
Console.WriteLine(Num1 * Num2)
break
}
else if (t == '/')
{
Num1 = Convert.ToDouble(r.Substring(0, y))
Num2 = Convert.ToDouble(r.Substring(y+1, r.Length - y))
Console.WriteLine(Num1 / Num2)
break
}
else
y++
y = 0
}
import java.io.BufferedReaderimport java.io.IOException
import java.io.InputStreamReader
public class EasyC {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str
BufferedReader in = new BufferedReader(new InputStreamReader(System.in))//能实现控制台输入的语句
try {
str = in.readLine()
int num = Integer.parseInt(str)
str = in.readLine()
int num1 = Integer.parseInt(str)
System.out.println("请输入运算符号:")
str = in.readLine()
if(str.length() == 1){
char c = str.charAt(0)
switch(c){
case '+':
System.out.println(num+num1)
break
case '-':
System.out.println(num-num1)
break
case '*':
System.out.println(num*num1)
break
case '/':
System.out.println(num/num1)
break
default:
System.out.println("请输入正确的运算符")
break
}
}
else{
System.out.println("请输入正确的运算符!")
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (NumberFormatException e){
System.out.println("输入的数不是数字")
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)