using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Assignment2
{
class Program
{
static void Main(string[] args)
{
int Value1,Value2,result,a;
Console.Write("Enter Value 1: ");
Value1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Value 2: ");
Value2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Choose an arithmetic operator:");
Console.WriteLine("\t1. +(add)");
Console.WriteLine("\t2. -(subtract)");
Console.WriteLine("\t3. *(multiply)");
Console.WriteLine("\t4. /(divide)");
Console.Write("\t5. %(mod)");
Console.Write("\n\n");
a = Convert.ToInt32(Console.ReadLine());
switch (a)
{
case"1":
Console.WriteLine("Operator is: Add");
result=Value1+Value2;
Console.WriteLine("Result: "+result);
break;
case"2":
Console.WriteLine("Operator is: Subtract");
result=Value1-Value2;
Console.WriteLine("Result: "+result);
break;
case"3":
Console.WriteLine("Operator is: Multiply");
result=Value1*Value2;
Console.WriteLine("Result: "+result);
break;
case"4":
Console.WriteLine("Operator is: devide");
result=Value1/Value2;
Console.WriteLine("Result: "+result);
break;
case"5":
Console.WriteLine("Operator is: mod");
result = Value1 % Value2;
Console.WriteLine("Result: "+result);
break;
}
}
}
}
我有基本的C#,我尝试制作一个可以使用Case语句运行5次算术运算的程序,在我完成编码后...发现了5个错误让我卡住了...输出总是带有2个结果(即我选择Add ..并且会出现2输出,它会同时加减。有人帮我这个吗?
答案 0 :(得分:2)
违规代码是您的案例陈述。 a
是int
。您的案例陈述都使用string
s。只需删除数字周围的引号:
switch(a)
{
case 1:
// some code
break;
case 2:
// some code
break;
// rest of cases
}
答案 1 :(得分:0)
您正在将a
转换为int,但您的switch语句将其与字符串匹配。将其更改为case 1:
而不是case "1"
以解决问题。
答案 2 :(得分:0)
删除引号以防万一。即。
将case "1":
替换为case 1: