gpt4 book ai didi

c# - 返回程序顶部再试一次

转载 作者:太空狗 更新时间:2023-10-30 01:22:58 25 4
gpt4 key购买 nike

我正在自学 C#,当前章节的挑战要求我:

以您的上一个项目为例,创建额外的方法来减去、乘以或除以传递给它的两个数字。在 divide 方法中,检查第二个数字是否不为 0,因为除以 0 是一个非法的数学概念。如果第二个数字是 0,则返回 0。

现在我写了下面我认为满足所有标准的内容。我不确定 IF 语句是否是最佳选择,但它确实有效。我还认为 SWITCH 也可以解决问题。

那么第一个问题,IF 或 SWITCH 哪个更好?

第二个问题。在 ELSE 中,如果用户没有选择可用选项之一,我会给出一条通用的失败消息,但我想做的是如果调用 ELSE(不确定正确的术语是什么),我希望程序返回开始并要求用户重试并显示第一个要求选择运算符(operator)的 Console.Writeline()。我知道这不是挑战的一部分,但它似乎是对程序的合乎逻辑的补充,我想知道这是否可能而不诉诸于任何太复杂的东西。

提前致谢!

        string whichOp;
int firstNum, secondNum, result;

Console.WriteLine("What Operator do you wish to use? [A]dd, [S]ubtract, [M]ultiply or [D]ivide?");

whichOp = Console.ReadLine();

whichOp = whichOp.ToLower();

if (whichOp == "a")
{
Console.Write("You chose Addition. Please choose your first number: ");
firstNum = int.Parse(Console.ReadLine());
Console.Write("You chose the number {0}. Please choose a second number: ", firstNum);
secondNum = int.Parse(Console.ReadLine());
result = Add(firstNum, secondNum);
Console.WriteLine("You chose the number {0}. {1} plus {2} equals {3}.", secondNum, firstNum, secondNum, result);
}
else if (whichOp == "s")
{
Console.Write("You chose Subtraction. Please choose your first number: ");
firstNum = int.Parse(Console.ReadLine());
Console.Write("You chose the number {0}. Please choose a second number: ", firstNum);
secondNum = int.Parse(Console.ReadLine());
result = Sub(firstNum, secondNum);
Console.WriteLine("You chose the number {0}. {1} minus {2} equals {3}.", secondNum, firstNum, secondNum, result);
}
else if (whichOp == "m")
{
Console.Write("You chose Multiplication. Please choose your first number: ");
firstNum = int.Parse(Console.ReadLine());
Console.Write("You chose the number {0}. Please choose a second number: ", firstNum);
secondNum = int.Parse(Console.ReadLine());
result = Mult(firstNum, secondNum);
Console.WriteLine("You chose the number {0}. {1} times {2} equals {3}.", secondNum, firstNum, secondNum, result);
}
else if (whichOp == "d")
{
Console.Write("You chose Division. Please choose your first number: ");
firstNum = int.Parse(Console.ReadLine());
Console.Write("You chose the number {0}. Please choose a second number: ", firstNum);
secondNum = int.Parse(Console.ReadLine());
result = Div(firstNum, secondNum);
Console.WriteLine("You chose the number {0}. {1} divided by {2} equals {3}.", secondNum, firstNum, secondNum, result);
}
else
Console.WriteLine("FAIL! You did not choose an available option.");

Console.ReadLine();
}

static int Add(int num1, int num2)
{
int theAnswer;

theAnswer = num1 + num2;

return theAnswer;
}

static int Mult(int num1, int num2)
{
int theAnswer;

theAnswer = num1 * num2;

return theAnswer;
}

static int Sub(int num1, int num2)
{
int theAnswer;

theAnswer = num1 - num2;

return theAnswer;
}

static int Div(int num1, int num2)
{
int theAnswer;

if (num2 == 0)
return 0;

theAnswer = num1 / num2;

return theAnswer;
}

编辑:我采纳了这里的建议,并用 SWITCH 和 WHILE 重建了程序。也有人说,因为很多代码都是一样的,所以我应该可以重用它。我喜欢这个想法,并将研究如何做到这一点。

        var retry = true;
while (retry)
{
retry = false;

string whichOp;
int firstNum, secondNum, result;

Console.WriteLine("What Operator do you wish to use? [A]dd, [S]ubtract, [M]ultiply or [D]ivide?");

whichOp = Console.ReadLine();

whichOp = whichOp.ToLower();

switch (whichOp)
{
case "a":
Console.Write("You chose Addition. Please choose your first number: ");
firstNum = int.Parse(Console.ReadLine());
Console.Write("You chose the number {0}. Please choose a second number: ", firstNum);
secondNum = int.Parse(Console.ReadLine());
result = Add(firstNum, secondNum);
Console.WriteLine("You chose the number {0}. {1} plus {2} equals {3}.", secondNum, firstNum, secondNum, result);
Console.ReadLine();
break;
case "s":
Console.Write("You chose Subtraction. Please choose your first number: ");
firstNum = int.Parse(Console.ReadLine());
Console.Write("You chose the number {0}. Please choose a second number: ", firstNum);
secondNum = int.Parse(Console.ReadLine());
result = Sub(firstNum, secondNum);
Console.WriteLine("You chose the number {0}. {1} minus {2} equals {3}.", secondNum, firstNum, secondNum, result);
Console.ReadLine();
break;
case "m":
Console.Write("You chose Multiplication. Please choose your first number: ");
firstNum = int.Parse(Console.ReadLine());
Console.Write("You chose the number {0}. Please choose a second number: ", firstNum);
secondNum = int.Parse(Console.ReadLine());
result = Mult(firstNum, secondNum);
Console.WriteLine("You chose the number {0}. {1} times {2} equals {3}.", secondNum, firstNum, secondNum, result);
Console.ReadLine();
break;
case "d":
Console.Write("You chose Division. Please choose your first number: ");
firstNum = int.Parse(Console.ReadLine());
Console.Write("You chose the number {0}. Please choose a second number: ", firstNum);
secondNum = int.Parse(Console.ReadLine());
result = Div(firstNum, secondNum);
Console.WriteLine("You chose the number {0}. {1} divided by {2} equals {3}.", secondNum, firstNum, secondNum, result);
Console.ReadLine();
break;
default:
Console.WriteLine("I'm sorry. {0} is not an available option. Please try again.", whichOp.ToUpper());
retry = true;
break;
}
}
}

static int Add(int num1, int num2)
{
int theAnswer;

theAnswer = num1 + num2;

return theAnswer;
}

static int Mult(int num1, int num2)
{
int theAnswer;

theAnswer = num1 * num2;

return theAnswer;
}

static int Sub(int num1, int num2)
{
int theAnswer;

theAnswer = num1 - num2;

return theAnswer;
}

static int Div(int num1, int num2)
{
int theAnswer;

if (num2 == 0)
return 0;

theAnswer = num1 / num2;

return theAnswer;
}

最佳答案

(1) switch 通常是表达这种分支的一种更简洁的方式。 switch 的主要限制是它仅适用于编译时常量值(常量变量、文字字符串、整数、枚举等)。在这种情况下,这似乎不是问题,因此 switch 可能是更简洁、更短代码的不错选择。在对性能敏感的代码中(这当然不是),单个开关可以比一堆 if 更快,因为程序将测试值并直接跳转到正确的情况,而不是针对每个 if 条件进行测试直到达到匹配。

(2) 一种简单的方法是将整个程序包装在一个循环中:

var retry = true;
while (retry)
retry = false;
// your program
else { // or default: if you're going with switch
...
retry = true;
}
}

关于c# - 返回程序顶部再试一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12416835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com