C# 局部变量-开关情况
我正在学习编程,我是一个非常初学者(两个月前开始)。 我正在做数学计算的c#练习。我们的教授用了一个if。。。else(Embricted)循环做这个练习。我想用一个开关的情况下,但我挣扎与局部变量 我理解这个问题:案例2和案例3不“知道”变量totalNumber和nbrSubAssy,因为它们来自案例1和案例2,所以它们被检测为未赋值 如果我仍然想使用开关箱,我能做些什么来解决它C# 局部变量-开关情况,c#,variables,switch-statement,local,C#,Variables,Switch Statement,Local,我正在学习编程,我是一个非常初学者(两个月前开始)。 我正在做数学计算的c#练习。我们的教授用了一个if。。。else(Embricted)循环做这个练习。我想用一个开关的情况下,但我挣扎与局部变量 我理解这个问题:案例2和案例3不“知道”变量totalNumber和nbrSubAssy,因为它们来自案例1和案例2,所以它们被检测为未赋值 如果我仍然想使用开关箱,我能做些什么来解决它 using System; namespace Denombrements { class Progr
using System;
namespace Denombrements
{
class Program
{
static long IntMultiplication(int startValue, int endValue)
{
long multiplication = 1;
for (int k = startValue; k <= endValue; k++)
multiplication *= k;
return multiplication;
}
static int UserSelection(String message)
{
int number = 0;
Console.Write(message);
try
{
number = int.Parse(Console.ReadLine());
Console.WriteLine();
}
catch
{
Console.WriteLine();
Console.WriteLine("Wrong input, enter an integer");
}
return number;
}
static void Main(string[] args)
{
char choice = '1';
while (choice != '0')
{
Console.WriteLine("Permutation ...................... 1");
Console.WriteLine("Arrangement ...................... 2");
Console.WriteLine("Combination ...................... 3");
Console.WriteLine("Quit ............................. 0");
Console.Write("Choice : ");
choice = Console.ReadKey().KeyChar;
Console.WriteLine();
switch(choice)
{
case '0':
Environment.Exit(0);
break;
case '1':
int totalNumber = UserSelection("Total number of elements to be taken into account");
long permutation = IntMultiplication(1, totalNumber);
Console.WriteLine(totalNumber + "! = " + permutation);
break;
case '2':
int nbrSubAssy = UserSelection("Total number of elements in the subassy to be taken into account");
long arrangement = IntMultiplication(totalNumber - nbrSubAssy + 1, totalNumber);
Console.WriteLine("A(" + totalNumber + "/" + nbrSubAssy + ") = " + arrangement);
break;
case '3':
long combination = arrangement / IntMultiplication(1, nbrSubAssy);
Console.WriteLine("C(" + totalNumber + "/" + nbrSubAssy + ") = " + combination);
break;
default:
Console.WriteLine("Wrong input");
break;
}
}
Console.ReadLine();
}
}
}
使用系统;
名称空间表示元素
{
班级计划
{
静态长整数乘法(整数起始值、整数结束值)
{
长乘法=1;
对于(int k=startValue;k在While
循环之前声明变量,它将在循环的整个生命周期中保留它的值,您可以访问旧值
char choice = '1';
int nbrSubAssy = 0;
int totalNumber = 0;
long arrangement = 0;
while (choice != '0')
{
// code ...
switch (choice)
{
case '0':
Environment.Exit(0);
break;
case '1':
totalNumber = UserSelection("Total number of elements to be taken into account");
long permutation = IntMultiplication(1, totalNumber);
Console.WriteLine(totalNumber + "! = " + permutation);
break;
case '2':
nbrSubAssy = UserSelection("Total number of elements in the subassy to be taken into account");
arrangement = IntMultiplication(totalNumber - nbrSubAssy + 1, totalNumber);
Console.WriteLine("A(" + totalNumber + "/" + nbrSubAssy + ") = " + arrangement);
break;
case '3':
long combination = arrangement / IntMultiplication(1, nbrSubAssy);
Console.WriteLine("C(" + totalNumber + "/" + nbrSubAssy + ") = " + combination);
break;
default:
Console.WriteLine("Wrong input");
break;
}
}
或者在我看来,在你需要它们的每种情况下,你都可以提出更好的解决方案。请编辑你的问题标题来总结你要问的问题。考虑“车刹车-地湿”的区别。“为什么我的车在地面上潮湿时需要更长的时间来刹车?”。后者总结了问题的内容,而前者只是一组关键字。在本例中,我只需将相应的行c&p到案例3。旁注:
int.Parse(Console.ReadLine())
不好。非常不好。请改用Int.TryParse
并使用循环-否则,每当用户输入无法解析为Int的内容时,您的方法将简单地返回0。您如何使用if
?它与switch
遵循相同的作用域规则……“c&p”=“复制和粘贴”