C# dos窗口程序foreach循环进行计算调试结果如下请问是什么问题

C# dos窗口程序foreach循环进行计算调试结果如下请问是什么问题,第1张

这是这段计算程序的代码:

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.BufferedReader

import 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("输入的数不是数字")

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11623610.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存