Visual Studio 2015社区c#计算器代码错误
本文关键字:计算器 代码 错误 社区 Studio 2015 Visual | 更新日期: 2023-09-27 18:09:07
我写了一个测试示例代码,我有问题。代码的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SEC3_LEC19 {
class Program {
static void Main(string[] args) {
int x, y;
string num1, num2, choice = "yes";
char op;
bool again = true;
while (again) {
Console.WriteLine("Enter two integers");
// First Number
Console.Write("Enter num1: ");
num1 = Console.ReadLine();
// Second Number
Console.Write("Enter num2: ");
num2 = Console.ReadLine();
// The Operator
Console.Write(
"Enter the operation [+ - * /]: ");
op = (char)Console.Read();
if (int.TryParse(num1, out x)) {
;
} else {
Console.WriteLine(
num1 + " is NaN val set to 0");
}
if (int.TryParse(num2, out y)) {
;
} else {
Console.WriteLine(
num2 + " is NaN val set to 0");
}
switch (op) {
case '+':
Console.WriteLine(
x + " + " + y + " = " + (x + y));
break;
case '-':
Console.WriteLine(
x + " - " + y + " = " + (x - y));
break;
case '*':
Console.WriteLine(
x + " * " + y + " = " + (x * y));
break;
case '/':
if (y == 0) {
Console.WriteLine(
"Division by zero not allowed!");
} else {
Console.WriteLine(
x + " / " + y + " = " + (x - y));
}
break;
default:
Console.WriteLine(
"Operator Unrecognized");
break;
}
// Offer user to try again
Console.Write("Go again? [yes/no]: ");
// Read user input [NOT WORKING]
choice = Console.ReadLine();
switch (choice.ToLower()) {
case "yes":
again = true;
break;
case "no":
again = false;
break;
default:
Console.WriteLine(
"Unrecognized choice!");
break;
}
}
// **********************************************
Console.WriteLine("Press any key to continue..");
Console.ReadKey();
}
}
}
代码通过控制台使用while循环,要求用户输入两个数字,然后输入一个运算符,然后执行并显示计算结果。如果他们继续询问用户是否愿意再试一次。它使用choice = Console.ReadLine()
语句。根据答案,代码要么继续while循环,要么跳出循环。不幸的是,编译器似乎跳过了选择部分,直接进入switch语句。在代码的顶部也有类似的语句,它们工作得很好。
这是因为您在第34行使用了Console.Read()。它读取一个字符,当您按ENTER键时,它也读取返回键。因此,在缓冲区中已经有一个键,它跳过ReadLine()并将其作为换行符读取
如果您将op
更改为字符串,并且您将ReadLine()
替换为Read()
,那么这将确保在输入流中没有挂起/隐藏字符。然后,开关检查op
是否为字符串"+"
, "-"
, "*"
..等等,而不是char '+'
, '-'
…等。
我也修复了你的缩进。请在以后的实现中遵循这种风格,以便更好地阅读和理解:
这个应该可以工作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SEC3_LEC19
{
class Program
{
static void Main(string[] args)
{
int x, y;
string num1, num2, choice = "yes";
string op; // changed to string
bool again = true;
while (again)
{
Console.WriteLine("Enter two integers");
Console.Write("Enter num1: ");
num1 = Console.ReadLine();
Console.Write("Enter num2: ");
num2 = Console.ReadLine();
Console.Write("Enter the operation [+ - * /]: ");
op = Console.ReadLine(); // Read line instead of Read char
if (!int.TryParse(num1, out x))
Console.WriteLine(num1 + " is NaN val set to 0");
if (!int.TryParse(num2, out y))
Console.WriteLine(num2 + " is NaN val set to 0");
switch (op)
{
// Changed cases
case "+": Console.WriteLine( x + " + " + y + " = " + (x + y)); break;
case "-": Console.WriteLine( x + " - " + y + " = " + (x - y)); break;
case "*": Console.WriteLine( x + " * " + y + " = " + (x * y)); break;
case "/":
if (y == 0)
Console.WriteLine("Division by zero not allowed!");
else
Console.WriteLine( x + " / " + y + " = " + (x - y));
break;
default:
Console.WriteLine("Operator Unrecognized"); break;
}
Console.Write("Go again? [yes/no]: ");
choice = Console.ReadLine();
switch (choice.ToLower())
{
case "yes": again = true; break;
case "no": again = false; break;
default: Console.WriteLine( "Unrecognized choice!"); break;
}
}
Console.WriteLine("Press any key to continue..");
Console.ReadKey();
}
}
}